JavaScript Comparisons

JavaScript provides different operators for comparing values. These operators are used to compare two values and return a boolean (true or false).

1. Equality (==) and Strict Equality (===)

  • Equality (==): Compares two values for equality after converting both to a common type (type coercion).
  • Strict Equality (===): Compares two values for equality without converting their types.
    console.log(5 == '5');  // true (type coercion)
    console.log(5 === '5'); // false (no type coercion)
    

    Try It Now

2. Inequality (!=) and Strict Inequality (!==)

  • Inequality (!=): Compares two values for inequality after converting both to a common type.
  • Strict Inequality (!==): Compares two values for inequality without converting their types.
    console.log(5 != '5');  // false (type coercion)
    console.log(5 !== '5'); // true (no type coercion)
    

    Try It Now

3. Greater Than (>) and Less Than (<)

  • Greater Than (>): Checks if the left operand is greater than the right operand.
  • Less Than (<): Checks if the left operand is less than the right operand.
    console.log(7 > 5);  // true
    console.log(3 < 5);  // true
    

    Try It Now

4. Greater Than or Equal To (>=) and Less Than or Equal To (<=)

  • Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less Than or Equal To (<=): Checks if the left operand is less than or equal to the right operand.
    console.log(5 >= 5); // true
    console.log(5 <= 7); // true
    

    Try It Now

5. Comparing Different Types

JavaScript will convert values to a common type when using non-strict comparison operators (==, !=). This can lead to unexpected results. It’s generally recommended to use strict comparison (===, !==) to avoid type coercion.

console.log('5' == 5);   // true (string is converted to number)
console.log('5' === 5);  // false (different types)

Try It Now

6. Special Cases in Comparisons

  • NaN is Not Equal to Itself: Use isNaN() to check if a value is NaN.
    console.log(NaN == NaN);  // false
    console.log(isNaN(NaN));  // true
    

    Try It Now

  • null and undefined Comparisons:
    • null and undefined are equal to each other but not to other values.
      console.log(null == undefined);  // true
      console.log(null === undefined); // false
      

      Try It Now

7. Objects and Arrays Comparisons

  • Objects and arrays are compared by reference, not by value.
    const obj1 = { a: 1 };
    const obj2 = { a: 1 };
    console.log(obj1 == obj2);  // false
    console.log(obj1 === obj2); // false
    

    Try It Now

  • Comparing the same reference returns true.
    const obj1 = { a: 1 };
    const obj2 = obj1;
    console.log(obj1 == obj2);  // true
    console.log(obj1 === obj2); // true
    

    Try It Now

8. Boolean Comparisons

  • true is converted to 1 and false to 0 in non-strict comparisons.
    console.log(true == 1);  // true
    console.log(false == 0); // true
    

    Try It Now

Summary

  • Use strict equality (===) for most comparisons to avoid unexpected results from type coercion.
  • Be cautious when comparing objects, arrays, and functions, as they are compared by reference.
  • Understand special cases like NaN and null/undefined to prevent bugs.

Properly understanding and using comparisons in JavaScript is essential for writing reliable and bug-free code.