Booleans and Comparisons

JavaScript ES2024 · ✓ verified by execution on

JavaScript uses booleans (true and false) to make decisions. You generate these by comparing values.

Strict Equality vs Loose Equality

JavaScript has two ways to check if values are equal. The strict equality operator === compares both the value and the type.

javascript
console.log(5 === 5);
Output
true

If you compare two different types using strict equality, it evaluates to false:

javascript
console.log(5 === '5');
Output
false

However, JavaScript also has a loose equality operator == that attempts to coerce types to match before comparing them. Let’s see what happens if we use == instead.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

console.log(5 == '5');
Output
true

Trap: Because == coerces types, 1 == true, '0' == false, and [] == 0 all evaluate to true! You should almost always use === to prevent massive bugs in your applications.

Why Loose Equality Exists

The loose equality operator (==) exists because JavaScript attempts to convert and compare operands that are of different types. That sounds convenient — 1 == '1' being true saves a conversion. The trouble is that the coercion rules are not a single simple rule but a table of them, and the surprising entries surface as bugs long after the comparison was written.

Use === unless you have a specific reason not to. It skips conversion altogether: different types are simply not equal. Objects are the exception to both. == and === compare objects by identity, not contents, so {} === {} is false — two separate objects that happen to look alike. Comparing their contents is something you have to do yourself.

5 === "5" Are types identical? false No (Number vs String) 5 == "5" Coerce to same type true Compare 5 === 5
Strict vs Loose Equality evaluation process

Logical Operators

You can combine multiple boolean conditions together using Logical operators.

AND (&&): Returns true only if BOTH sides are true. Let’s step through this evaluation visually:

javascript · visualize
console.log(true && false);
false

OR (||): Returns true if AT LEAST ONE side is true.

javascript
console.log(true || false);
Output
true

NOT (!): Inverts the boolean.

javascript
console.log(!true);
Output
false

Warning: There is one value in JavaScript that breaks all equality rules. NaN === NaN evaluates to false! It is the only value in the language that is not strictly equal to itself.

Check yourself

Which operator should you use to check if two values are exactly identical in both value and type?

Reveal answer

=== — The strict equality operator (===) checks both type and value.

What is the result of true && false?

Reveal answer

false — Logical AND (&&) requires both conditions to be true.

Is NaN === NaN true or false?

Reveal answer

false — NaN is the ONLY value in JavaScript that is not strictly equal to itself!

Challenges

Challenge 1 +20 XP

Use the strict equality operator to check if a is equal to b.

javascript
  • Test 1 — expects "true"
Need a hint? (−25% XP)

Use === between a and b.

Show solution (0 XP)
const a = 10;
const b = 10;
const res = a === b;
console.log(res);

Challenge 2 +20 XP

Use the logical AND operator so that res evaluates to true.

javascript
  • Test 1 — expects "true"
Need a hint? (−25% XP)

Use && between condition1 and condition2.

Show solution (0 XP)
const condition1 = true;
const condition2 = true;
const res = condition1 && condition2;
console.log(res);