What's the reason to use === instead of == with typeof in Javascript?

后端 未结 5 1854
执笔经年
执笔经年 2020-11-29 09:23

Throughout many third-party libraries and best practices blogs/recommendations, etc... it is common to see syntax like this:

typeof x === \'object\' (instead         


        
相关标签:
5条回答
  • 2020-11-29 09:37

    Triple equal operators are mostly used for variable type and value checking (all in 1 expression), also known as equality without type coercion.

    Example:

    var a = 1;
    var b = 1;
    var c = "1";
    var d = "1";
    
    alert (a === b); //True, same value and same type (numeric)
    alert(c === d); //True, same value and same type (string)
    alert(b === c); //False, different type but same value of 1
    

    See Doug Crockford's YUI Theater on type coercion.


    If the typeof operator already returns a string, what's the need to type check the return value as well? If typeof(typeof(x)) is always string, no matter what x actually is, then == should be sufficient and === unnecessary.

    The most efficient reason for not using typeof and rather the === operator would be for type coercion (interpretation) between browsers. Some browsers can pass 6=="6" as true and some wouldn't (depending on the strictness of the JS interpreter) so by introducing type coercion would clarify this. Also, it would bring the "Object-Orientativity" approach to it since JavasScript's variables are not type-based variables (i.e. variable types are not declared on compile time like in Java).

    E.g. in Java, this would fail:

    if ("6" instanceof Number) { // false
    

    Hope I answered your question.

    0 讨论(0)
  • 2020-11-29 09:45

    Because === is quicker than ==, due to omitting type coercion. Sure it is probably a negligible difference but it is still there.

    0 讨论(0)
  • 2020-11-29 09:46

    There's no reason at all to favour === over == in this case, since both operands are guaranteed to be strings and both operators will therefore give the same result. Since == is one character fewer I would favour that.

    Crockford's advice on this is to use === all the time, which is reasonable advice for a beginner but pointlessly paranoid if you know the issues (covered in other answers).

    0 讨论(0)
  • 2020-11-29 09:56

    To answer the main question - there is no danger in using typeof with ==. Below is the reason why you may want to use === anyway.


    The recommendation from Crockford is that it's safer to use === in many circumstances, and that if you're going to use it in some circumstances it's better to be consistent and use it for everything.

    The thinking is that you can either think about whether to use == or === every time you check for equality, or you can just get into the habit of always writing ===.

    There's hardly ever a reason for using == over === - if you're comparing to true or false and you want coercion (for example you want 0 or '' to evaluate to false) then just use if(! empty_str) rather than if(empty_str == false).


    To those who don't understand the problems of == outside of the context of typeof, see this, from The Good Parts:

    '' == '0'          // false
    0 == ''            // true
    0 == '0'           // true
    
    false == 'false'   // false
    false == '0'       // true
    
    false == undefined // false
    false == null      // false
    null == undefined  // true
    
    ' \t\r\n ' == 0    // true
    
    0 讨论(0)
  • 2020-11-29 10:00

    If the typeof operator already returns a string, what's the need to type check the return value as well? If typeof(typeof(x)) is always string, no matter what x actually is, then == should be sufficient and === unnecessary.

    It's subjective. You can just as easily turn this around, and ask, "Why would you use == when you don't expect implicit conversions?" Both work fine here, so use the one you feel expresses your intention better. Try to be consistent within a project.

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