Javascripts variables are dynamically typed. While == operator converts each to a string representation before comparision, === compares without converting. For example,
1 == '1'; // true
1 === '1'; // false
1 == [1]; // true
1 === [1]; // falsePrimitive Data Types
- undefined
- null
- boolean
- number: includes Infinity and NaN
- string
- symbol
- object: includes arrays
Truthy and Falsy
- The following values are always falsy:
- false
- 0
- ’’ or “”
- null
- undefined
- NaN
- Everything else is truthy.
Loose Equality Comparisons With ==
- false, 0, ‘’, and “” are all equivalent.
- null and undefined are equivalent.
- NaN is not equivalent to anything, even itself.
- Infinity and {} is not equivalent to anything excep itself.
- [] is equivalent to false, 0 and ‘’.
Strict Equality Comparisons With ===
Except NaN is not quivalent to ifseft, anything equivalent to itself but not others.
Quiz 1
What is the result of executing this code?
var a = 0 || [] || 1;
var b = NaN == NaN;
console.log(a, b);Quiz 2
Suppose that I want to call foo() when x and y are identical. The following code is correct?
if (x === y) {
foo();
}If not, please fix it!