Two equality operators
This lesson exists because of one historical decision: JavaScript was built in 1995 to be forgiving on web pages, so its original == converts types before comparing. That leniency hides bugs — form input always arrives as strings, and a string that merely looks like your number should not count as equal to it. Modern code opts out of the conversion entirely.
JavaScript has two ways to ask "are these equal?":
===(strict equality) checks value and type. If the types differ, the answer isfalse, end of story.==(loose equality) first converts the values toward a common type, then compares. That conversion produces famous surprises like"5" == 5beingtrue.
Python's == behaves like JavaScript's ===, so your instinct transfers with one edit: always type three equals signs. The same goes for not-equal: use !==, not !=.
5 === 5 // true "5" === 5 // false, string vs number "5" == 5 // true, but never rely on this
Professional JavaScript codebases ban == outright. We will use === everywhere in this course.
Code exercise · javascript
Predict all four lines, then run. Remember: === refuses to compare across types, while == converts first.
Truthiness: the six falsy values
When JavaScript needs a yes-or-no answer from a value (inside an if, which you will write in lesson 3-1), it converts the value to a boolean. Exactly six values convert to false:
false 0 "" null undefined NaN
Everything else is truthy, including "0" (a non-empty string) and negative numbers. You can test any value yourself with the Boolean(...) function, which performs that exact conversion.
Python works similarly (0, "", None are falsy), so the idea is familiar. The value worth memorizing is NaN, "not a number", which appears when math goes wrong, for example Number("abc").
Code exercise · javascript
Your turn. Use Boolean(...) to print the truthiness of these four values, one per line: 0, "0", "", 42. Before running, predict which of the four is the odd one out.
Code exercise · javascript
Second practice. Predict all three lines before running, then print: 3 === 3, then "3" !== 3, then Boolean("false"). The last one is the classic trap.
Quiz
Coming from Python, which JavaScript operator behaves like the Python == you already know?