JavaScript · The language round
Which values are truthy and falsy in JavaScript?
Short answer
The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else, including empty arrays and empty objects, is truthy.
Conditions such as if, &&, || and the ternary operator coerce their operands to booleans. Knowing the exact falsy set prevents a lot of bugs.
Boolean(false); // false
Boolean(0); // false
Boolean(-0); // false
Boolean(0n); // false
Boolean(""); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean(NaN); // false
Boolean([]); // true
Boolean({}); // true
Boolean("0"); // true
Boolean("false"); // trueEmpty collections are truthy
An empty array is an object, and every object is truthy. The same is true for an empty object. If you need to check whether an array is empty, check its length.
const items = [];
if (items) {
console.log("This runs");
}
if (items.length) {
console.log("This does not run");
}|| returns the first truthy operand, while ?? returns the right side only for null or undefined. That difference matters when 0 or "" are valid values.
new Boolean(false) is an object, so it is truthy. Avoid the object wrappers and use primitive booleans.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Is an empty array truthy?
- 2What is the difference between `||` and `??`?
- 3Is the string `"0"` truthy?
- 4Why is `new Boolean(false)` truthy?
Say it in the interview
The falsy values are false, zero, negative zero, zero BigInt, the empty string, null, undefined, and NaN. Everything else is truthy, including empty arrays and empty objects. That is why checking an array directly is not an emptiness check; I check length. I also use nullish coalescing instead of logical OR when zero or an empty string are valid values.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.