!=
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