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
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.
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 :â(
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.
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,
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?
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.).