20 Tricky Truthy and Falsy Values Questions in JavaScript
In JavaScript, the concept of truthy and falsy values plays a critical role in how the language evaluates expressions in conditional statements like if and while. Understanding how JavaScript interprets values is essential to writing cleaner and more predictable code.
Here are 20 tricky truthy and falsy questions designed to help you enhance your understanding of JavaScript's behavior in different conditions.
1. What will the following code log to the console?
if ('0') {
console.log('True');
} else {
console.log('False');
}Answer: True
- A non-empty string, including
'0', is considered truthy in JavaScript.
2. What will be the output of this code?
if ([]) {
console.log('True');
} else {
console.log('False');
}Answer: True
- An empty array
[]is truthy in JavaScript.
3. What is the result of this comparison?
console.log(null == undefined);Answer: True
nullandundefinedare loosely equal (==) but not strictly equal (===).
4. What will be the output of this code?
if (NaN) {
console.log('True');
} else {
console.log('False');
}Answer: False
NaN(Not-a-Number) is falsy in JavaScript.
5. What will this code log to the console?
if (false) {
console.log('True');
} else {
console.log('False');
}Answer: False
- The value
falseis falsy.
6. What will this log to the console?
if (0) {
console.log('True');
} else {
console.log('False');
}Answer: False
- The number
0is falsy.
7. What will be the result of this comparison?
console.log('' == 0);Answer: True
- An empty string
''is loosely equal to0(==), but not strictly equal (===).
8. What will be logged here?
if ('false') {
console.log('True');
} else {
console.log('False');
}Answer: True
- Any non-empty string, including
'false', is truthy.
9. What will this code log to the console?
if (undefined) {
console.log('True');
} else {
console.log('False');
}Answer: False
undefinedis falsy.
10. What will this log?
console.log([] == []);Answer: False
- Arrays are reference types, so two different arrays are not loosely equal.
11. What will this code log to the console?
if (' ') {
console.log('True');
} else {
console.log('False');
}Answer: True
- A string containing a space
' 'is truthy.
12. What will the following code output?
console.log(0 == false);Answer: True
0andfalseare loosely equal (==).
13. What will the following code output?
console.log([] == false);Answer: True
- An empty array
[]is loosely equal tofalse.
14. What will this code log?
if ({} == false) {
console.log('True');
} else {
console.log('False');
}Answer: False
- An empty object
{}is not loosely equal tofalse.
15. What will be the result of this code?
if (null) {
console.log('True');
} else {
console.log('False');
}Answer: False
nullis falsy.
16. What will this log?
console.log(1 == true);Answer: True
1is loosely equal totrue.
17. What will the following code print?
if (true) {
console.log('True');
} else {
console.log('False');
}Answer: True
trueis truthy.
18. What will this code output?
console.log('0' == 0);Answer: True
- The string
'0'is loosely equal to the number0.
19. What will this log to the console?
if (0 == '0') {
console.log('True');
} else {
console.log('False');
}Answer: True
0and'0'are loosely equal (==).
20. What will this code print?
if (false == '') {
console.log('True');
} else {
console.log('False');
}Answer: True
falseis loosely equal to an empty string''.
Summary of Key Falsy and Truthy Values in JavaScript:
- Falsy values:
false,0,""(empty string),NaN,null,undefined. - Truthy values: Everything that is not falsy (e.g., non-empty strings, non-zero numbers, objects, arrays).
These questions are designed to help you better understand how JavaScript evaluates values in conditional statements and expressions. Mastering this knowledge is essential for writing effective and bug-free code in JavaScript.
Thank you for reading!
I hope this blog helped you understand tricky truthy and falsy values in JavaScript. Stay tuned for more programming tips and insights.
Follow for more: shubhadipbhowmik
