support@90-10.dev

Comparison Operators

Equality

Javascript provides two types of equality comparison operators:

"Loose" Equality Operator (==)

The comparison is true if the operands are equal after type conversion:

1 == 1     // true
1 == '1'   // true
1 == true  // true
0 == false // true

Using the "loose" equality operator can lead to unexpected results:

null == undefined  // true

0 == ''            // true
0 == []            // true

1 == ["1"]         // true
1 == "hello"       // true
1 == true          // true
true == "hello"    // false

Strict Equality Operator (===)

The comparison is true if the operands are strictly equal with no type conversion:

1 === 1   // true
1 === '1' // false

null === undefined  // false

It is generally recommended to use strict equality comparisons whenever possible, as they are more reliable and avoid unexpected type coercion. However, there may be situations where loose equality comparisons are necessary or more convenient.

Not Equal Operators

In a similar fashion we have loose & strict non-equal operators:

1 != "1"      // false
1 !== "1"     // true
1 != "true"   // false
1 !== "true"  // true

Other Comparison Operators

Other comparison operators are: less than (<), less than or equal to (<=), greater than (>) and greater than or equal to (>=):

const x = 1
x < 1    // false
x <= 1   // true
x > 1    // false
x >= 1   // true

For other operators, see the JavaScript overview page.