Javascript check for undefined quotes

Discussion in 'Learning Javascript' started by SilverDash, Nov 23, 2015.

  1. SilverDash

    SilverDash Veteran Veteran

    Messages:
    389
    Likes Received:
    151
    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: Nov 23, 2015
    #1
  2. Iavra

    Iavra Veteran Veteran

    Messages:
    1,797
    Likes Received:
    856
    First Language:
    German
    #2
  3. Mellye

    Mellye Veteran Veteran

    Messages:
    347
    Likes Received:
    277
    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: Nov 23, 2015
    #3
  4. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    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: Nov 24, 2015
    #4
  5. SilverDash

    SilverDash Veteran Veteran

    Messages:
    389
    Likes Received:
    151
    First Language:
    Dutch
    Primarily Uses:
    RMMV
    Oh I see the typeof is what changes everything. Thanks all.
     
    #5

Share This Page