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:
false
-
0
and-0
-
""
(empty string) null
undefined
NaN
-
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
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
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
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)
what do you think ?
This gif makes me sick
sorry for that, I'm trying different things
Some comments have been hidden by the post's author - find out more