Every decision a program makes comes down to a question with a yes or no answer. Is the password long enough? Has the player run out of lives? Is the basket empty? Those answers are booleans, true or false, and they usually come from comparing two values.
Comparisons look simple, but JavaScript has a few quirks around types that cause real bugs. This lesson explains why you should always use ===, how strings are compared, and which values JavaScript treats as "false" even though they aren't the boolean false.
Comparison operators
| Operator | Means | 5 ? 3 |
|---|---|---|
=== | equal (same value and same type) | false |
!== | not equal | true |
> | greater than | true |
< | less than | false |
>= | greater than or equal to | true |
<= | less than or equal to | false |
Every comparison produces a boolean. You can store it in a variable, such as const isAdult = age >= 18;, which often makes the code that follows easier to read.
=== vs ==: strict and loose equality
== converts types before comparing, which leads to strange results:
"5" == 5 // true (the string is converted to a number)
0 == "" // true (surprising!)
null == undefined // true=== never converts. Values of different types are simply not equal. Always use === and !==, and convert values yourself with Number() or String() when you need to compare different types.
Comparing strings
Strings are compared character by character using each character's code, so "apple" < "banana" is true. Capital letters come before lowercase ones, which means "Z" < "a" is also true. Convert both sides to lowercase before sorting or comparing words alphabetically.
Numbers stored as text compare as text too: "10" < "9" is true, because "1" comes before "9". Convert them to numbers first.
Truthy and falsy values
In an if statement, and with &&, || and !, every value behaves like a boolean. Only six values are falsy:
false, 0, "" (an empty string), null, undefined and NaN
Everything else is truthy, including "0", "false", [] and {}. Use Boolean(value) or !!value to check how a value behaves.
Comparing objects, arrays and special values
Comparison operators check identity for objects and arrays, not their contents. [1, 2] === [1, 2] is false, because they're two separate arrays that happen to look the same. Two variables are only equal when they point to the very same object. To compare contents, compare individual values, or for simple data use JSON.stringify(a) === JSON.stringify(b).
A few special values need care too. NaN is not equal to anything, not even itself, so use Number.isNaN(x) to test for it. null == undefined is true with loose equality but false with ===. And comparing a number with a string, as in "10" > 9, converts the string to a number first, which works here but hides bugs elsewhere.
JavaScript Comparisons Syntax
Comparisons
age >= 18 // true or false
name === "Sam" // strict equality
score !== 0 // strict inequality
Boolean("") // false
!!"hello" // trueJavaScript Comparisons Summary
- Comparisons return booleans:
trueorfalse. - Use
===and!==, which never convert types. - Strings compare character by character, with capitals before lowercase letters.
- Only
false,0,"",null,undefinedandNaNare falsy. - Every other value, including
"0"and empty arrays, is truthy.
JavaScript Comparisons Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Strict vs loose equality
console.log(5 === 5);
console.log("5" === 5);
console.log("5" == 5);
console.log(0 == "");
console.log(null === undefined, null == undefined);Line-by-line explanation
- Line 1Same type and value: true.
- Line 2String vs number: false with
===. - Lines 3 to 4
==converts types, giving surprisingtrues. - Line 5Another
==quirk. Stick with===.
Truthy and falsy values
const values = [0, 1, "", "0", "hi", null, undefined, NaN, [], {}];
const labels = ['0', '1', '""', '"0"', '"hi"', 'null', 'undefined', 'NaN', '[]', '{}'];
for (let i = 0; i < values.length; i++) {
console.log(labels[i], "→", Boolean(values[i]));
}Line-by-line explanation
- Line 1A mix of values to test.
- Line 2Readable labels for each value.
- Lines 4 to 6Print each label and whether its value is truthy. Notice
"0",[]and{}are truthy.
Real-world checks
const password = "sunflower7";
const username = "";
const age = 16;
const longEnough = password.length >= 8;
const hasName = Boolean(username);
const isTeen = age >= 13 && age <= 19;
console.log("Password OK:", longEnough);
console.log("Has username:", hasName);
console.log("Teenager:", isTeen);
console.log("apple" < "banana", "Zebra" < "apple");Line-by-line explanation
- Line 5A comparison gives a boolean.
- Line 6An empty string is falsy, so this is false.
- Line 7Two comparisons combined with AND for a range check.
- Line 12String comparison is alphabetical, but capitals come first.
Common JavaScript Comparisons Mistakes
Using == with user input
if (input == 0) { … } // also true for "" and "0"if (Number(input) === 0) { … }Thinking "false" is falsy
const setting = "false";
if (setting) { console.log("This runs!"); }const setting = "false";
if (setting === "true") { … }Any non-empty string is truthy, even the word "false".
JavaScript Comparisons in Real Projects
Booleans drive interfaces
Buttons are disabled while isLoading is true, menus open when isMenuOpen is true, and forms submit only when every check like emailIsValid && passwordIsLongEnough is true. Naming booleans with is, has or can makes code read like English.
JavaScript Comparisons Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Use strict comparisons.
const pin = 1234;
console.log(pin 1234); // true
console.log(pin 1111); // true
=== checks equality; !== checks inequality, both without converting types.What does this print?
console.log(Boolean("0"), Boolean(0), Boolean([]));Which of these is falsy?
The check should log Access: true only when the code typed is exactly the number 42. The input arrives as a string. Fix it so it uses strict equality correctly.
const typed = "42";
const access = typed = 42;
console.log("Access:", access);- Logs "Access: true"
- access is a boolean
- No errors
= assigns. Convert the string, then compare with ===.JavaScript Comparisons Quiz
3 questions to check what you learned.
What does
10 >= 10return?- false
- true
- 10
- undefined
What does
"3" === 3return?- true
- NaN
- false
- An error
How many falsy values are there in JavaScript?
- 6 (false, 0, "", null, undefined, NaN)
- 2
- 4
- Every value is falsy
JavaScript Comparisons Challenge
Ride height checker
A theme park ride needs riders to be at least 120cm and under 200cm.
- Create
const canRide = height >= 120 && height < 200; - Log
Can ride: truefor the given height - Also create
const hasTicket = Boolean(ticketCode);and logHas ticket: false
const height = 134;
const ticketCode = "";
- canRide is true
- hasTicket is false
- Logs both lines