Lesson 18: Mastering JavaScript Comparisons with challenges!

manoj ymkmanoj ymk
3 min read

🧠 Internal Mechanics

1. Returns Boolean

All comparisons return true or false.

2. String Comparison

  • Lexicographical order (like dictionary).

  • Case-sensitive.

  • Based on Unicode values.

'Z' > 'A'         // true
'apple' > 'pineapple' // false ('a' < 'p')
'2' > '12'        // true (because '2' > '1' in Unicode)

3. Different Types β†’ Coercion (except with ===)

'2' > 1       // true β†’ '2' gets converted to number 2
false == 0    // true β†’ both converted to 0
true == 1     // true β†’ both converted to 1

4. Strict Equality ===

  • No coercion.

  • Compares both value and type.

0 === false   // false
'5' === 5     // false

⚠️ Hidden Quirks and Gotchas

⚑ null and undefined

ExpressionResultReason
null == undefinedtrueSpecial case in spec
null === undefinedfalseDifferent types
null > 0falsenull β†’ 0, but not greater
null == 0falsenull β‰  number
null >= 0truenull β†’ 0, so 0 β‰₯ 0
undefined > 0falseundefined β†’ NaN
undefined == 0falseundefined only equals null loosely

πŸ“Œ Strict equality is safer.


πŸ§ͺ Real-world Usage

  • Form field values are usually strings. Comparing them to numbers can lead to tricky bugs.

  • Defaulting behavior often checks == null to cover both null and undefined.

  • Use === in most situations to avoid surprises.

  • In conditionals, prefer truthiness/falsiness with care:

if (value != null) {
  // value is not null or undefined
}

🧩 Debugging Scenarios

  1. "2" > "12" returns true
    β†’ Because it's string comparison: '2' > '1' lexicographically.

  2. null >= 0 is true, but null > 0 is false
    β†’ Comparison uses numeric coercion but in subtly different ways.

  3. undefined == 0 is false, but false == 0 is true
    β†’ false β†’ 0, but undefined only equals null in loose equality.


πŸ’Ό Interview Prep Notes

βœ… Do:

  • Use === and !== by default.

  • Check value == null to test for both null and undefined.

❌ Don’t:

  • Use == unless you really know what it does.

  • Compare potentially null/undefined values with <, >, <=, >= without safe-guards.


🧠 Mnemonics

  • "== is loose, === is truth" – use === for strict, bug-free checks.

  • "Falsy values can trick you!" – always test explicitly.


🎯 Tasks Recap (with Explanations)

5 > 4                // true       β†’ obvious
"apple" > "pineapple" // false      β†’ 'a' < 'p'
"2" > "12"           // true       β†’ '2' > '1' (string comparison)
undefined == null    // true       β†’ only values that are loosely equal to each other
undefined === null   // false      β†’ different types
null == "\n0\n"      // false      β†’ "\n0\n" becomes 0, null β‰  0
null === +"\n0\n"    // false      β†’ "\n0\n" β†’ 0 β†’ type mismatch with null

🧠 Bonus Mind Map

                   Comparisons in JS
                        |
     -----------------------------------------
    |                  |                      |
   Strict            Loose                Type Coercion
  (===, !==)         (==, !=)             -> Number for most ops
    |                  |
 Type + Value      Only Value             ⚠️ Tricky with null/undefined
 Match Needed      Tries to Convert
0
Subscribe to my newsletter

Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

manoj ymk
manoj ymk