Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between != and !== [duplicate]

Tags:

javascript

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;

}

like image 767
Mirko Martic Avatar asked Feb 28 '17 20:02

Mirko Martic


People also ask

What is the difference between != and !== In JS?

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

Is != The same as ==?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

Why do we prefer === and !== Over == and != In JavaScript?

The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done, and the types must be the same to be considered equal. The == operator will compare for equality after doing any necessary type conversions.

What is the difference between != and not?

The != operator compares the value or equality of two objects, whereas the Python is not operator checks whether two variables point to the same object in memory.


2 Answers

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

like image 149
Justin Taddei Avatar answered Sep 18 '22 06:09

Justin Taddei


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

The following are true:

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

The following are NOT true:

false === null
false === undefined
false === 0
2 === "2"
like image 41
Red Twoon Avatar answered Sep 20 '22 06:09

Red Twoon