What are falsy values in JS? What are truthy values in JS?

Question

What are falsy values in JS? What are truthy values in JS?

Answer

A falsy value is one that evaluates to the boolean value false - expressions, statements, and values can evaluate to the false boolean value and will therefore be considered a falsy value. Some falsy values to remember:
false
0 (or other forms of the number 0, -0, 0.0, etc)
empty strings ('', "", etc)
null
undefined
NaN

Other values are considered truthy; a value that evaluates to the boolean value true - which can also be expressions, statements, and values. Some truthy values to keep in mind:
non-empty strings (like "false", ' ' - including strings with just whitespace)
-1 (or other negative and non-zero numbers)
{} (empty objects)
[] (empty arrays)
function(){}, () => {} (functions, including empty functions)

  • do keep in mind JavaScript’s type conversion when using the equality, ==, operator as this operator will convert the number 0 and/or string "0" to the same type if either is an operand of ==.
    Example: "0" == 0 //evaluates to true
3 Likes

Hi. I have question? can someone take a look and tell where I am doing wrong please

?

Maybe they want the three ===

I did but it still shows that it’s not correct

It does say to return false if the “value is falsy”, maybe they want you to not use else but an explicit else if, or if (sky != 'blue')

The exercise expects us to test for truthiness, not an explicit value.

 const truthyOrFalsy(value) {
    if (value) {
        return true;
    }
    return false;
 }

Depending on whether or not value is not zero, not the empty string, not null, not undefined, and not NaN, the above will return accordingly. We do not want to match to an explicit value, but test the truthiness of the input value.

26 Likes

in this code it says if there is a value return true but it didn’t determine which typeof values ! and then there is the second return without if or else startments? how come?
i solved it with determining if the value was so and so and so but yeah it was long and then i review the solution and couldn’t get it, please explain :’(

The type is of no concern. There are only a handful of values that will be falsy…

0
-0
0n
''
""
null
undefined
NaN
false

All others, regardless of type are truthy.

When the if truthy branch contains a return, thus ends the conditional. When it is not truthy it falls through to the last line.

3 Likes

oooh! so when the condition is falsy the return fails to return and goes to the second return :smiley: thanks!

1 Like

You are testing for default boolean values associated with values of other data types. If whatever is typed in place of parameter has truthy value, the function should return true.

Any valid string will return true.
Any number other than 0 will return true.

Absent parameter will return false.
Any undefined variable or null object will return false.
0 will return false.

Ternary operator combined with concise body arrow function offers a nice one-line solution.

const truthyOrFalsy = parameter => parameter ? true : false;

7 Likes

I want to know, if we reach the same conclusion (i.e., it returns false), but the challenge still does not pass through, does that mean we are good to go and the code is good block to use (however not as to standards the challenge requires us)? Or should it be reworked? I did it using this and had the console return false, but didn’t pass the challenge.

Blockquote // if (value === false || value === 0 || value === ‘0n’ || value === “” || value === null || value === //undefined || value === NaN) {
// return false;
// } else
// return true;
// };
Blockquote

Welcome to the forums, @a0uney. Seems you have covered all the bases that are falsy. Would it not be simpler to check if a value is truthy, and avoid all the verbose logic?

We know that NOT, i.e., ! will negate a value’s boolean evaluation, so,

! false  ===  true 

and likewise,

! ! false  === false

Notice how we toggled it back to its original boolean state.

Given a value, say 1, how would we express that as true?

Answer: ! ! 1

which becomes true.

const truthyOrFalsy = value => ! ! value;

As it happens, if () {} always evaluates the condition in boolean terms, so,

if (value) {
    return true
}
return false
7 Likes

function truthyOrFalsy(value){
value ? true : false
}

Why does the above not work, but const truthyOrFalsy = value => value ? true : false; does?

Because in curly braces the return is explicit

{
return
}
=> value ? true : false;   // return is implicit
3 Likes

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)


Hello, mtf.
I dont understand what is wrong with my code :confused: . It says Nan is not definded.

I think this might clear my misunderstanding with the exercise, but just to make sure I’m understanding this correctly, is this why we wouldn’t use a comparison?

i.e " if (value = true) {return true} "

Firstly, the assignment operator (=) is not the same as the comparison operators (==/===). value = true is assigning the value of true to the variable value. It is not comparing whether or not value == true (is truthy) or value === true (exactly equal to true).

if (value == true) is the same thing as if (value), but the second method is much more concise and can be preferable to the first. That is what the following line refers to.


However, if we want to compare if (value === true), we could not use if (value), since any truthy value will make if (value) true when we only want to see if what is stored in value is exactly true.

Note: truthy values are any values that are considered to be true when evaluated as booleans (1 is truthy and 0 is falsy, true is truthy and false is falsy, a non-empty string is truthy and an empty string is falsy, etc.).

1 Like

Nan is undefined, but NaN is. Check the spelling.

Which would be rarely. it’s not a comparison that needs to be explicit since the yield is still the same.

1 Like