Operators and strings · Lesson 6 of 45
Comparison and logic
===, !==, &&, || and ??.
8 minutes4 games
Comparisons return true or false: >, <, >=, <=, === (exactly equal) and !== (not equal).
Always use ===, not ==. == converts types before comparing and causes surprises, like 0 == "" being true.
JavaScript
console.log(5 === 5) // true
console.log(5 === "5") // false: different types
console.log(5 == "5") // true: converted, avoid
console.log("apple" < "banana") // true: dictionary orderLogical operators
| Operator | Meaning | Example |
|---|---|---|
| and: both must be true |
|
| or: one is enough |
|
| not |
|
| fallback if |
|
JavaScript
const age = 20 const hasTicket = true console.log(age >= 18 && hasTicket) // true const nickname = null console.log(nickname ?? "Guest") // Guest const count = 0 console.log(count || 10) // 10: 0 is falsy console.log(count ?? 10) // 0: only null/undefined are replaced
What's the output?
console.log(true && false) console.log(true || false) console.log(!true)
What will this code print?
Quick question
What is "3" === 3?
Complete the code
Print true if the age is from 13 to 19 (a teen).
const age = 16 console.log(age >= 13 age <= 19)
Exercise
Print username, or Anonymous if it's missing (use ??).
Expected output: Anonymous
JavaScript
const username = undefined
Learn more from
Played 0 of 4 games
Sign in to save your progress and earn XP.