It seems to me that the operators == and === work the same way. For example:
1 == 1 // true 1 === 1 // true 1 == 2 // false 1 === 2 // false The operator == shorter and is often found in other programming languages. So why === at all?
It seems to me that the operators == and === work the same way. For example:
1 == 1 // true 1 === 1 // true 1 == 2 // false 1 === 2 // false The operator == shorter and is often found in other programming languages. So why === at all?
Operators === and !== are strict comparison operators. Therefore, if the operands are of different types, they are not equal. For example:
1 === "1" // возвращает false 1 !== "1" // возвращает true null === undefined // возвращает false Operators == and != Are operators of, say, a rough comparison. That is, if the operands are of different types, JavaScript tries to somehow convert them to become comparable. For example:
1 == "1" // возвращает true 1 != "1" // возвращает false null == undefined // возвращает true It is interesting to note that unlike === , the == operator is not transitive :
"0" == 0 // true 0 == "" // true "0" == ""// false It is not very easy to remember the rules of this rough comparison, and sometimes it happens that it works in unexpected ways. Therefore, I recommend using === instead of == .
Even I do not remember the fine details of the operator == , so let's see in the specification, clause 11.9.3 :
Abstract Equality Comparison Algorithm
Comparing x == y, where x and y are values, returns true or false. This comparison is made as follows:
- If Type (x) is the same as Type (y), then
- If Type (x) is Undefined, return true.
- If Type (x) is Null, return true.
- If Type (x) is Number, then
- If x is NaN, return false.
- If y is NaN, return false.
- If x has the same numeric value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type (x) is String, then return true if x and y have exactly the same sequence of characters (of the same length and with the same characters in the corresponding positions). Otherwise, return false.
- If Type (x) is Boolean, return true if x and y are both true or false. Otherwise, return false.
- Return true if x and y belong to the same object. Otherwise, return false.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type (x) is Number and Type (y) is String,
return the result of the comparison x == ToNumber (y).- If Type (x) is String and Type (y) is Number,
return the result of the comparison ToNumber (x) == y.- If Type (x) is Boolean, return the result of the comparison ToNumber (x) == y.
- If Type (y) is Boolean, return the result of the comparison x == ToNumber (y).
- If Type (x) is either String or Number, and Type (y) is Object,
return the result of the comparison x == ToPrimitive (y).- If Type (x) is Object and Type (y) is either a String or Number,
return the comparison result ToPrimitive (x) == y.- Return false.
=== : for example, in the expression typeof a === typeof b it is quite enough == . And situations in which there is confidence in the type of operands are not so rare. - Regent=== everywhere than to think every time, "is it enough == ?" - Peter OlsonSource: https://ru.stackoverflow.com/questions/431662/
All Articles
==in general? - VladD