Differences between == and === (comparisons in JavaScript)

75

I've always seen that in JavaScript there is:

  • assignments =
  • comparisons == and ===

I think I understand that == does something similar to compare the value of the variable and the === also compares the type (like an equals of java).

Could someone confirm this point and extend it? . I'm javero and I do not type in javascript sometimes I love it and sometimes I hate it.

What is the correct way in javascript to compare undefined , null and other default values?

variable == null
variable === null

Is undefined used as a text string or as a keyword? Which of the following comparisons is correct for an element html without value ? (for example, a label without content)

variable == "undefined"
variable === "undefined"
variable == undefined
variable === undefined
    
asked by Jordi Castilla 03.12.2015 в 10:45
source

7 answers

70

The operators === and !== are the comparison operators strict . This means that if the operands have different types, they are not the same. For example,

1 === "1" // false
1 !== "1"  // true
null === undefined // false

The operators == and != are the comparison operators relaxed . That is, if the operands have different types, JavaScript tries to convert them to be comparable. For example,

1 == "1" // true
1 != "1" // false
null == undefined // true

It is worth mentioning that the operator == is not transitive , unlike === .

"0" == 0 // true
0 == ""  // true
"0" == ""// false

It is not very easy to remember all the rules of relaxed comparison, and sometimes it works in a counterintuitive way. Therefore, I recommend using === instead of == .

I do not remember all the little details of the operator == , so let's take a look at the specification, point 11.9.3:

  

The abstract equality comparison algorithm

     

The comparison x == and , where x e and are values, return true or false . This comparison is made as follows:

     
  • If Type (x) and Type (y) are the same,      
  • If Type (x) is Undefined , return true .
  •   
  • If Type (x) is Null , return true .
  •   
  • If Type (x) is Number ,      
  • If x is NaN , return false .
  •   
  • If and is NaN , return false .
  •   
  • If x is the same value of Number as and , return true .
  •   
  • If x is +0 e and is -0 , returns true .
  •   
  • If x is -0 e and is +0 , returns true .
  •   
  • Returns false .
  •      
  • If Type (x) is String , return true if x e and are exactly the same sequence of characters (with the same length and the same characters in the corresponding positions). Otherwise, return false .
  •   
  • If Type (x) is Boolean , return true , if x e and both are true or both are false . Otherwise, return false .
  •   
  • Returns true if x e and refer to the same object. Otherwise, return false .
  •      
  • If x is null e and is undefined , return true .
  •   
  • If x is undefined e and is null , return true .
  •   
  • If Type (x) is Number and Type (y) is String ,
      returns the result of the comparison x == ToNumber (y) .
  •   
  • If Type (x) is String and Type (y) is Number ,
      returns the result of the comparison ToNumber (x) == and .
  •   
  • If Type (x) is Boolean , return the result of the comparison ToNumber (x) == and .
  •   
  • If Type (y) is Boolean , return the result of the comparison x == ToNumber (y) .
  •   
  • If Type (x) is String or Number , and Type (y) is Object ,
      returns the result of the comparison x == ToPrimitive (y) .
  •   
  • If Type (x) is Object and Type (y) is String or Number ,
      returns the result of the comparison ToPrimitive (x) == and .
  •   
  • Returns false .
  •   

    This answer is a translation of my answer to the same question on the Russian site .

        
    answered by 04.12.2015 / 02:51
    source
    39

    The difference is that == first try to convert the types before comparing them. The operator === no, does a direct comparison but always returns false if the types are different. source (in English)

    ex:

    1 == "1" // verdadero    
    1 === "1" // falso
    '' == 0 // verdadero
    '' === 0 // falso
    

    These graphs may help you visualize the difference:

    Operator '=='

    Operator '==='

    ( source of images )

        
    answered by 03.12.2015 в 10:56
    33

    Indeed == compares value, while === compares also type. For example.

    "1" == 1 // true
    "1" === 1 // false
    null == undefined // true
    null === undefined // false
    

    You can read the technical details (in English) at Equality comparisons and sameness

    undefined is a reserved word and is used without quotes.

        
    answered by 03.12.2015 в 10:57
    24

    Not exactly. It is true that the operator === compares the type and the value, without making conversions as the operator == would. However, that is only true for types of value. In the types by reference what it does is to verify that it is the same object, it is not enough that it be of the same type and value. So === would be an identity operator, unlike == .

    For example:

    var a = { x: 1, y: 2 };
    var b = { x: 1, y: 2 };
    var c = a;
    
    a === b  // false
    a === c  // true
    

    The case of strings would work as a type of value, unless you used the String constructor, which would be a reference type.

    Also point out that the operator == is not transitive in some conversions. For example:

    '' == '0'  // false
    0 == ''    // true
    

    Given the ambiguity of this operator, it is advised that the === operator be used whenever possible.

    In any case you can take a look at this page that will answer questions about both operators. There you can check among other things that undefined == null but undefined !== null .

        
    answered by 03.12.2015 в 11:30
    8

    Very simple

    == is equality of value and not of type.
    === is equality of value and type.

    1 == '1'  // true
    1 === '1' // false, ya que uno es un string.
    1 === 1   // son iguales 
    
        
    answered by 03.09.2016 в 23:27
    7

    They have already answered the question correctly, but I would like to comment on what seems to be your problem (I also started with java and there are certain things that javascript facilitates).

    A variable can have 2 states, "undefined" or have something, null as in SQl is a type of value, meaning that if your variable has null it is defined and has something, nothing but something: P, undefined means that your variable has not been defined, it is curious that fact above all because null == undefined responds true.

    Now when you search for an element with js and can not find it (for example document.getElementById ("inpt")) its value is null, in the case that if you find it by its value and if you do not have it Answer "".

    The interesting part is that with a simple if you solve a lot.

    var una,dos = null,tres =[],cuatro = 1;
    
    if(noexisto) //es undefinided, saca error por que estas preguntando por ella
    if( typeof notoy == "undefined") //es la manera correcta de preguntar si una variable no ha sido definida
    if(una)//false pero no saca error por que implicitamente tiene un null
    if(una == null) //es lo mismo que la anterior
    if(dos) //false pero no saca error
    if(tres) //true por que contiene un array, vacio pero es algo
    if("")//false por que esta vacio
    if(cuatro)//tiene algo por tanto es true
    
        
    answered by 03.12.2015 в 21:15
    2

    In case of not transpiling your JS code (with Babel for example) the operation "==" is faster than the type check with "===" therefore if your code is going to be useful in many occasions (it is part of the architecture or some library) it is preferable to use "==" taking into account that you are not going to pass a type filter. On the other occasions (the vast majority) it is better to use type checking with "===" to avoid unexpected results.

        
    answered by 03.11.2016 в 15:08