26

In this function, when it is compared the lenght of the array it is used != operator and when it is comparing all elements of the array it is using !== operator. Why?! Thx.

var a = [1,2,3];
var b = [2,3,4];

function equalArrays(a,b){      
    if(a.length != b.length) return false;
    for(var i = 0; i < a.length; i++)
        if(a[i] ==! b[i]) return false;
    return true;

}

Mirko Martic
  • 263
  • 1
  • 3
  • 4
  • 2
    This is a "if `a[i]` **is** equal to "**not** `b[i]`" And makes little sense in a `equalArrays()` function – Maximilian Gerhardt Feb 28 '17 at 20:04
  • 2
    Please see [this](http://stackoverflow.com/questions/523643/difference-between-and-in-javascript) post and [this](http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) one for a comparison of `==` and `===`; the `!=` and `!==` are just the respective negated comparisons. – Alexander Nied Feb 28 '17 at 20:05
  • 1
    Please double-check your code. Is it `!==` or `==!`. – PM 77-1 Feb 28 '17 at 20:05
  • The difference is that != is for comparing two different types for example: if you are comparing a variable and a string but it will still work if you comparing a string and a string or a var and a var and what a !== does is only compares the same type so it wont work with a var and a string – Pritam Prince Dec 13 '17 at 06:20

5 Answers5

47

= is an assignment operator, e.g. If you run var x = 1; then x will have the value of 1.

== (or !=) is a comparison operator that checks if the value of something is equal to the value of something else. e.g. if(x == 1) will evaluate to true and so will if(x == true) because 1 will evaluate to true and 0 evaluate to false.

=== (or !==) is another comparison operator that checks if the value of something is equal to the value of, and is the same type as something else. e.g. if(x === 1) will evaluate to true however, if(x === true) will evaluate to false because 1 (the value of x) is an integer and true is a boolean.

Justin Taddei
  • 2,142
  • 1
  • 16
  • 30
22

The triple equals (===) not only checks the value, but the type.

The following are true:

false == false
false == 0
2 == "2"

The following are NOT true:

false === null
false === undefined
false === 0
2 === "2"

coming back to this years later... had to edit my response, because the following

false != undefined
false != null
null == undefined
Red Twoon
  • 685
  • 4
  • 14
8

!= will only check value regardless of operands type. but !== is used to compare both value & type of 2 operands that are being compared to each other.

When its comparing the length of arrays its obvious that both of them are integer so there is no need to compare their types. But in order to compare the elements in the array their types are important. For example assume its comparing string of 5 and integer 5:

if( '5' !== 5 ){
    return false
}else{
    return true;
}

The above snippet will return false cause two operands are off different types. But this can not be caught by !=, I mean:

if( '5' != 5 ){
    return false;
}else{
    return true;
}

will return true.

As a rule of thumb, remember that:

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.

  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.

  • Two Boolean operands are strictly equal if both are true or both are false.

  • Two objects are strictly equal if they refer to the same Object.

  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Quoted from: https://stackoverflow.com/a/523647

Pang
  • 9,564
  • 146
  • 81
  • 122
behkod
  • 2,647
  • 2
  • 18
  • 33
1

!==means that two variables are being checked for both their value and their value type (8!==8 would return false while 8!=="8" returns true). != only checks the variable's value (8!=8 and 8!="8" would both return false).

Ali Camilletti
  • 116
  • 1
  • 3
  • 11
0

The !== operator returns true when elements are not equal value or not equal type.

Source: W3Schools