Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.6 — Equality and Truthiness
JavaScript · Example 4 of 4

Truthy and falsy values

The first two lines are the eight falsy values. Everything on the third line is truthy.

Use === to compare values without hidden conversion, Object.is for the two cases === gets wrong (NaN and -0), and learn the eight falsy values, because every if converts its condition to true or false.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Log whether the string "10" and the number 10 are strictly equal, then convert the string so that the strict comparison is true.

02

Write a check that logs missing when a variable discount is null or undefined, but not when it is 0. Test it with all three values.

03

Log the result of Boolean for these values and explain any surprise: "0", [], NaN, " ".

Explain it without notes

01

What is the difference between == and ===? Which do you use and why?

02

Why is [] == false true, while [] is truthy?

03

What does Object.is do differently from ===?

04

List the falsy values in JavaScript. What's a common bug caused by truthiness checks?

Truthy and falsy values

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

false false false false
false false false false
true true true true true true
No name given