20 Tricky Truthy and Falsy Values in JavaScript with Solutions

November 10, 2024 (5mo ago)

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


2. What will be the output of this code?

if ([]) {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


3. What is the result of this comparison?

console.log(null == undefined);

Answer: True


4. What will be the output of this code?

if (NaN) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


5. What will this code log to the console?

if (false) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


6. What will this log to the console?

if (0) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


7. What will be the result of this comparison?

console.log('' == 0);

Answer: True


8. What will be logged here?

if ('false') {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


9. What will this code log to the console?

if (undefined) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


10. What will this log?

console.log([] == []);

Answer: False


11. What will this code log to the console?

if (' ') {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


12. What will the following code output?

console.log(0 == false);

Answer: True


13. What will the following code output?

console.log([] == false);

Answer: True


14. What will this code log?

if ({} == false) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


15. What will be the result of this code?

if (null) {
  console.log('True');
} else {
  console.log('False');
}

Answer: False


16. What will this log?

console.log(1 == true);

Answer: True


17. What will the following code print?

if (true) {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


18. What will this code output?

console.log('0' == 0);

Answer: True


19. What will this log to the console?

if (0 == '0') {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


20. What will this code print?

if (false == '') {
  console.log('True');
} else {
  console.log('False');
}

Answer: True


Summary of Key Falsy and Truthy Values in JavaScript:

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

shubhadip bhowmik

#javascript#programming#web-development#tutorial