Javascript check for undefined quotes

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
// In Game_Eventtypeof this.page() === undefined // doesn't worktypeof this.page() === 'undefined' // works// also requires quotes around the undefined.(typeof Silv.Minimap.POI[$gameMap._mapId] === 'undefined')// But then in DataManager.onLoad:if (data && data.note !== undefined) // <<<< no quotes! huh?Erm... huh? When to quote undefined?
 
Last edited by a moderator:

Mellye

Veteran
Veteran
Joined
Oct 24, 2015
Messages
347
Reaction score
279
First Language
Portuguese
The typeof operator always returns a string. So if you're using it to check the type of something, you need to compare it to 'undefined' (with quotemarks, as it is a string).

When you compare something directly to undefined (without quotemarks, and without using typeof), you're actually just comparing it to a global variable that doesn't exist - one that is undefined. So two undefined things comparing to each other will return true.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
use typeof instead or instanceof. If you know how to pull in third party libraries, use lodashes isUndefined function
 
Last edited by a moderator:

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
Oh I see the typeof is what changes everything. Thanks all.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,049
Members
137,570
Latest member
fgfhdfg
Top