Advertisement
JavaScript provides two types of equality operators:
- Loose equality (==, !=): Performs type conversion if the types differ, comparing values after converting them to a common type.
- Strict equality (===, !==): Compares both value and type, without any type conversion.
#### Strict Equality (===)
- Two strings are strictly equal if they have exactly the same sequence of characters and length.
- Two numbers are strictly equal if they have the same numeric value.
- Special cases:
- NaN === NaN is false
- +0 === -0 is true
- Two booleans are strictly equal if both are true or both are false.
- Two objects are strictly equal if they refer to the same object in memory.
- null and undefined are not strictly equal.
#### Loose Equality (==)
- Converts operands to the same type before making the comparison.
- null == undefined is true.
- "1" == 1 is true because the string is converted to a number.
- 0 == false is true because false is converted to 0.
#### Examples:
10 == false // true (loose equality, type coercion)
2 0 === false // false (strict equality, different types)
3 1 == "1" // true (string converted to number)
4 1 === "1" // false (different types)
5
6null == undefined // true (special case)
7
8null === undefined // false (different types)
9'0' == false // true ('0' is converted to 0)
10'0' === false // false (different types)
11
12NaN == NaN // false (NaN is never equal to itself)
13
14NaN === NaN // false
15[] == [] // false (different array objects)
16[] === [] // false
17{} == {} // false (different object references)
18{} === {} // falseAdvertisement
JavaScript Coding Exercise 10
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement