variable === undefined vs. typeof variable === “undefined”

后端 未结 8 1234
情深已故
情深已故 2020-11-22 14:43

The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined.

  • Global Variables: typeof variable === "undefined&
相关标签:
8条回答
  • 2020-11-22 15:32

    Yet another reason for using the typeof-variant: undefined can be redefined.

    undefined = "foo";
    var variable = "foo";
    if (variable === undefined)
      console.log("eh, what?!");
    

    The result of typeof variable cannot.

    Update: note that this is not the case in ES5 there the global undefined is a non-configurable, non-writable property:

    15.1.1 Value Properties of the Global Object
    [...]
    15.1.1.3 undefined
    The value of undefined is undefined (see 8.1). This property has the attributes
    { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

    But it still can be shadowed by a local variable:

    (function() {
      var undefined = "foo";
      var variable = "foo";
      if (variable === undefined)
        console.log("eh, what?!");  
    })()
    

    or parameter:

    (function(undefined) {
      var variable = "foo";
      if (variable === undefined)
        console.log("eh, what?!");  
    })("foo")
    
    0 讨论(0)
  • 2020-11-22 15:36

    For local variables, checking with localVar === undefined will work because they must have been defined somewhere within the local scope or they will not be considered local.

    For variables which are not local and not defined anywhere, the check someVar === undefined will throw exception: Uncaught ReferenceError: j is not defined

    Here is some code which will clarify what I am saying above. Please pay attention to inline comments for further clarity.

    function f (x) {
        if (x === undefined) console.log('x is undefined [x === undefined].');
        else console.log('x is not undefined [x === undefined.]');
    
        if (typeof(x) === 'undefined') console.log('x is undefined [typeof(x) === \'undefined\'].');
        else console.log('x is not undefined [typeof(x) === \'undefined\'].');
    
        // This will throw exception because what the hell is j? It is nowhere to be found.
        try
        {
            if (j === undefined) console.log('j is undefined [j === undefined].');
            else console.log('j is not undefined [j === undefined].');
        }
        catch(e){console.log('Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.');}
    
        // However this will not throw exception
        if (typeof j === 'undefined') console.log('j is undefined (typeof(x) === \'undefined\'). We can use this check even though j is nowhere to be found in our source code and it will not throw.');
        else console.log('j is not undefined [typeof(x) === \'undefined\'].');
    };
    

    If we call the above code like this:

    f();
    

    The output would be this:

    x is undefined [x === undefined].
    x is undefined [typeof(x) === 'undefined'].
    Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.
    j is undefined (typeof(x) === 'undefined'). We can use this check even though j is nowhere to be found in our source code and it will not throw.
    

    If we call the above code like these (with any value actually):

    f(null); 
    f(1);
    

    The output will be:

    x is not undefined [x === undefined].
    x is not undefined [typeof(x) === 'undefined'].
    Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.
    j is undefined (typeof(x) === 'undefined'). We can use this check even though j is nowhere to be found in our source code and it will not throw.
    

    When you do the check like this: typeof x === 'undefined', you are essentially asking this: Please check if the variable x exists (has been defined) somewhere in the source code. (more or less). If you know C# or Java, this type of check is never done because if it does not exist, it will not compile.

    <== Fiddle Me ==>

    0 讨论(0)
提交回复
热议问题