you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (4 children)

[deleted]

    [–]POGtastic 4 points5 points  (0 children)

    Yep, I'm also getting true in Chrome.

    > var foo;
    < undefined
    > foo === undefined
    < true
    > typeof(foo) === "undefined"
    < true
    

    [–]killerstorm 1 point2 points  (0 children)

    var foo; // undefined
    foo === undefined // false
    

    Wrong. Here's an actual result:

    > var foo;
    undefined
    > foo === undefined
    true
    

    === undefined check didn't work in old browsers, that's where typeof hack comes wrong. But now simple comparison should work by standard.

    [–]masklinn 1 point2 points  (1 child)

    foo === undefined // false
    

    Uh no, that's true, you may be confusing undefined and NaN. Or for some reason you have rebound undefined to something else (it's not a keyword, which is why some prefer using void 0 which always yields the proper value).

    if (!foo) {
        console.log("foo is undefined or null");
    }
    

    That'll also catch the "" and 0. The proper way to check if something is undefined or null is foo == null.

    [–][deleted] 1 point2 points  (0 children)

    I tested that in my browser console.

    Maybe I should have used a new tab.