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, since 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 compare for equality:
| Operator | Name | Compares | "5" vs 5 |
|---|---|---|---|
=== | strict equality | value and type, no conversion | false |
== | loose equality | converts first, then compares | true |
Python's == behaves like JavaScript's ===, so your instinct transfers with one edit: always type three equals signs. The same goes for not-equal, where you use !== rather than !=.
5 === 5 // true "5" === 5 // false, string vs number "5" == 5 // true, but never rely on this
Professional JavaScript codebases ban == outright, and this course uses === everywhere.
The four comparisons that show the difference
Four lines, covering both operators and both outcomes. The pattern to watch is that === refuses to compare across types while == converts first.
console.log(5 === 5); console.log("5" === 5); console.log("5" == 5); console.log(10 !== "10");
Output
true false true true
Line one is true because the values and types both match. Line two is false purely because of type: the characters look identical, but a string is not a number. Line three flips to true only because == converted "5" into 5 before looking. Line four is true for the same reason line two was false, since !== reports a difference and a type difference counts.
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").
Four values through Boolean(...)
The Boolean(...) function performs exactly the conversion an if performs, so it is the cleanest way to inspect truthiness directly. Here it is applied to four values, two of which are falsy.
console.log(Boolean(0)); console.log(Boolean("0")); console.log(Boolean("")); console.log(Boolean(42));
Output
false true false true
The odd one out is "0", and it catches almost everyone. The number 0 is falsy, but "0" is a string containing one character, and every string with at least one character in it is truthy. The empty string "" is falsy because it has no characters, and 42 is truthy because every number except 0 and NaN is.
A strict inequality and a misleading string
Three comparisons, ending with the trap that catches the most people.
console.log(3 === 3); console.log("3" !== 3); console.log(Boolean("false"));
Output
true true true
The first line is a plain match. The second is true because !== reports a difference in value or in type, and a string never strictly equals a number. The third line is the interesting one: Boolean("false") is true, because "false" is a non-empty string and only the empty string "" is falsy. The letters inside a string have no bearing on its truthiness, so a text value of "false" read from a config file or a form will happily pass an if check unless you compare it explicitly.
The operator that matches your Python instinct
Coming from Python, the JavaScript operator that behaves like the == you already know is ===, because it compares value and type without converting either side.
Python's == never silently turns a string into a number, and neither does ===. JavaScript's == does exactly that conversion, which is why "5" == 5 is true and why style guides ban the operator. The two remaining lookalikes are easy to keep straight: a single = is assignment rather than a comparison at all, and !== is the strict form of not-equal.
Rule for the rest of the course, and for real code: type
===and!==every time. If you ever genuinely need loose comparison, an explicit conversion such asNumber(input) === 5says what you mean and leaves nothing for the next reader to guess.