Difference Between =, == and === in JavaScript

What is = in JavaScript?

Equal to (=) is an assignment operator which sets the variable on the left of the = to the value of the expression that is on its right. This operator assigns lvalue to rvalue.


Why use = in JavaScript?

The = JavaScript operator assigns a value to the left operand depends on the value of operand available on the right side. The first operand should be a variable. ++The basic assignment operator is = that assigns the value of one operand to another. That is a = b assigns the value of b to a.

-------------------------

What is == in JavaScript?

Double equals (==) is a comparison operator which transforms the operands having the same type before comparison. So when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number) which returns false.


Why use == in JavaScript?

The == operator is an equality operator. It checks whether its two operands are the same or not by changing expression from one data type to others. You can use == operator in order to compare the identity of two operands even though they are not of a similar type.

-------------------------

What is === in JavaScript?

=== (Triple equals) is a strict equality comparison operator in JavaScript which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with "2" using === then it will return a false value.


Why use === in JavaScript?

Strict equality === checks that two values are the same or not.

Value are not implicitly converted to some other value before comparison.

If the variable values are of different types, then the values are considered as unequal.

If the variable are of the same type are not numeric and have the same value they are considered as equal.

Lastly, If both variable values are numbers, they are considered equal if both are not NaN (Not a Number) and are the same value.

No comments:

Post a Comment