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

4 1 2 1

🔥 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
Fyodor •
Comment hidden by post author
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

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay