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

后端 未结 8 1225
情深已故
情深已故 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:11

    typeof a === 'undefined' is faster then a === 'undefined' by about 2 times on node v6.9.1.

    0 讨论(0)
  • 2020-11-22 15:12

    I'd stick to using typeof foo === "undefined" everywhere. That can never go wrong.

    I imagine the reason why jQuery recommends the two different methods is that they define their own undefined variable within the function that jQuery code lives in, so within that function undefined is safe from tampering from outside. I would also imagine that someone somewhere has benchmarked the two different approaches and discovered that foo === undefined is faster and therefore decided it's the way to go. [UPDATE: as noted in the comments, the comparison with undefined is also slightly shorter, which could be a consideration.] However, the gain in practical situations will be utterly insignificant: this check will never, ever be any kind of bottleneck, and what you lose is significant: evaluating a property of a host object for comparison can throw an error whereas a typeof check never will.

    For example, the following is used in IE for parsing XML:

    var x = new ActiveXObject("Microsoft.XMLDOM");
    

    To check whether it has a loadXML method safely:

    typeof x.loadXML === "undefined"; // Returns false
    

    On the other hand:

    x.loadXML === undefined; // Throws an error
    

    UPDATE

    Another advantage of the typeof check that I forgot to mention was that it also works with undeclared variables, which the foo === undefined check does not, and in fact throws a ReferenceError. Thanks to @LinusKleen for reminding me. For example:

    typeof someUndeclaredVariable; // "undefined"
    someUndeclaredVariable === undefined; // throws a ReferenceError
    

    Bottom line: always use the typeof check.

    0 讨论(0)
  • 2020-11-22 15:13

    Who is interested in the performance gain of variable === undefined, may take a look here, but it seems to be a chrome optimization only.

    • http://jsperf.com/type-of-undefined-vs-undefined/30
    • http://jsperf.com/type-of-undefined-vs-undefined
    0 讨论(0)
  • 2020-11-22 15:21

    Because undefined is not always declared, but jQuery declares undefined in its main function. So they use the safe undefined value internally, but outside, they use the typeof style to be safe.

    0 讨论(0)
  • 2020-11-22 15:25

    For undeclared variables, typeof foo will return the string literal "undefined", whereas the identity check foo === undefined would trigger the error "foo is not defined".

    For local variables (which you know are declared somewhere), no such error would occur, hence the identity check.

    0 讨论(0)
  • 2020-11-22 15:27

    Summary:

    When at global scope we actually want to return true if the variable is not declared or has the value undefined:

    var globalVar1;
    
    // This variable is declared, but not defined and thus has the value undefined
    console.log(globalVar1 === undefined);
    
    // This variable is not declared and thus will throw a referenceError
    console.log(globalVar2 === undefined);

    Because in global scope we are not 100% sure if a variable is declared this might give us a referenceError. When we use the typeof operator on the unknown variable we are not getting this issue when the variable is not declared:

    var globalVar1;
    
    console.log(typeof globalVar1 === 'undefined');
    console.log(typeof globalVar2 === 'undefined');

    This is due to the fact that the typeof operator returns the string undefined when a variable is not declared or currently hold the value undefined which is exactly what we want.


    • With local variables we don't have this problem because we know beforehand that this variable will exist. We can simply look in the respective function if the variable is present.
    • With object properties we don't have this problem because when we try to lookup an object property which does not exist we also get the value undefined

    var obj = {};
    
    console.log(obj.myProp === undefined);

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