DEV Community

Cover image for 🔥 What are falsey values in JavaScript? 10 Must-Know Questions for Junior Developers💡🚀
Mohammed Awad
Mohammed Awad

Posted on • Edited on

🔥 What are falsey values in JavaScript? 10 Must-Know Questions for Junior Developers💡🚀

In JavaScript, "falsy" values are values that are treated as false in Boolean contexts, such as conditional statements (if, while), logical operations (&&, ||, !), and other areas where a Boolean value is expected. There are seven falsy values in JavaScript:

  1. false
  2. 0 and -0
  3. "" (empty string)
  4. null
  5. undefined
  6. NaN
  7. document.all (for historical web compatibility reasons)

Any value that is not in this list is considered "truthy". Here is an example demonstrating how JavaScript treats falsy values:

const test1 = "" // empty string
const test2 = 0 // zero
const test3 = parseFloat("hi") // NaN 

if(!test1 && !test2 && !test3){
  console.log("falsy")
} // logs falsy
Enter fullscreen mode Exit fullscreen mode

In this example, test1, test2, and test3 are all falsy values, so !test1 && !test2 && !test3 returns true, and 'falsy' is logged to the console Source 8.

It's important to note that while falsy values are treated as false in Boolean contexts, they are not the same as the Boolean value false. For example, if you use the strict equality operator === to compare a falsy value with false, the result will be false, because === checks both the value and the type. Here's an example:

const test1 = "" === false // test1 is false
Enter fullscreen mode Exit fullscreen mode

In this case, test1 is false because the empty string "" and false are different types (String and Boolean, respectively) Source 8.

You can convert any JavaScript value to a Boolean value using the Boolean function or the !! (double NOT) operator. This will return true for truthy values and false for falsy values:

const value = ""; // a falsy value
console.log(Boolean(value)); // false
console.log(!!value); // false

const value2 = "hello"; // a truthy value
console.log(Boolean(value2)); // true
console.log(!!value2); // true
Enter fullscreen mode Exit fullscreen mode

now after you know what falsy values record yourself answering that question and repeating it until you succeed because explaining what you know is very important for employers or HR.

I would love to share a part of my preparation tips for job interview.


As a React developer, I'm currently on the lookout for new opportunities. If you know of any roles where my experience could be a good fit, I would love to hear from you.

You can reach out to me anytime at my email xMohammedAwad@gamil.com, or connect with me on LinkedIn. Check out my projects on GitHub to see more examples of my work.

Top comments (3)

Collapse
 
xmohammedawad profile image
Mohammed Awad

what do you think ?

Collapse
 
fyodorio profile image
Info Comment hidden by post author - thread only accessible via permalink
Fyodor

This gif makes me sick

Collapse
 
xmohammedawad profile image
Mohammed Awad

sorry for that, I'm trying different things

Some comments have been hidden by the post's author - find out more