Predict Coercion in Conditions
Predict Output Medium
Question
What does this code print?
Code
const values = [0, "", null, undefined, NaN, false, [], {}];
for (const v of values) {
console.log(Boolean(v));
}
Hint
JavaScript has 6 falsy values: 0, '', null, undefined, NaN, false.
Explanation
The 6 falsy values in JavaScript are: 0, "" (empty string), null, undefined, NaN, and false. Everything else is truthy, including [] (empty array) and {} (empty object). So the output is: false, false, false, false, false, false, true, true (for the 8 values in order).
Related Resources
Related Lessons
- Conditions
Learn about truthy and falsy values.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.