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

Consider,

const findTrueIndices = (arr) => {
  const result = [];
  arr.forEach((value, index) => {
    if (value === true) {
      result.push(index);
    }
  });
  return result;
}


const test = findTrueIndices([false, false, true, true, false, true, false, true]);
// should return [2, 3, 5, 7]
console.log(test);

When this satisfies the condition…

    if (value)

There really has to be a use case requirement, else it’s all potato, potato, tomato, tomato.

Thank you so much! Your comment helped a lot!

1 Like

I was a bit confused at the beginning, but thanks to @mtf comment saying “The exercise expects us to test for truthiness, not an explicit value.” it helped me to understand the issue.

I misread the exercise and I didn’t interpret that “any value” should go inside the function.

This was my solution:

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

For some reason it de-formatted my code while using “quote”, sorry about that.

1 Like

you should write a function that checks for any value not only
if (sky == "blue") return true; else return false;
in the above if statement you only are checking if the passed argument is “blue” or not.
we could use ternary operator like this:

return input ? true : false;

The above code is saying if the input is truthy return true otherwise return false.

I was expecting this solution to work, however, it doesn’t pass the exercise:

function truthyOrFalsy (value) {
return value == true;
}

It returns all values correctly in the Codecademy editor except non-empty strings, such as ‘hello’, which return false.

The above function behaves the same as if I had written:

function truthyOrFalsy (value) {
if (value == true) {
return true;
}
return false;
}

However, if I change the function to the below it works:

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

Can you explain why that might be, since like you I thought that if (value == true) is the same as if (value), and therefore return value == true should work as well? Is this just a glitch in the Codecademy editor?

value cannot be coerced to true in a direct comparison. The only value that can equal true is, true. We are concerned with the truth value, regardless of type.

I read your reply, but I don’t really understand it.

I am logging the following to the console to test the functions:

console.log(truthyOrFalsy(false)) // Should print false
console.log(truthyOrFalsy(0)) // Should print false
console.log(truthyOrFalsy(-0)) // Should print false
console.log(truthyOrFalsy(’’)) // Should print false
console.log(truthyOrFalsy(null)) // Should print false
console.log(truthyOrFalsy(undefined)) // Should print false
console.log(truthyOrFalsy(NaN)) // Should print false
console.log(truthyOrFalsy(1)) // Should print true
console.log(truthyOrFalsy(‘hello’)) // Should print true
console.log(truthyOrFalsy(true)) // Should print true

Can you explain why the following 2 functions:

function truthyOrFalsy (value) {
if (value == true) {
return true;
}
return false;
}

AND

function truthyOrFalsy (value) {
return value == true;
}

return this output to the console:

false
false
false
false
false
false
false
true
false
true

While the following function that you posted in the thread:

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

returns this output to the console instead:

false
false
false
false
false
false
false
true
true
true

Why the difference in the second to the last output for this statement:

console.log(truthyOrFalsy(‘hello’)) // Should print true

Shouldn’t if (value == true) be the same as if (value), and the functions return the same output? I understand that return value == true is behaving the same as if (value == true), but I don’t know why if (value == true) returns the wrong output, while if (value) returns the correct output in the editor?

We don’t know what TYPE a value will be. Only if it is already a boolean, true, can we make a direct comparison to the primitive, true. Regardless what truthyness it may hold, a truthy value will always fail a direct comparison unless it is, literally, true.

"a" === true

<- false

What the expression,

if (value === true) { }

could be transformed to is a logical, rather than relational expression.

if (value && true) { }

The only way to get access to the code block is if value is truthy.

That brings us down to the simplification,

if (value) { }

which does the job nicely.

By using the == operator, instead of the === operator, I don’t believe that I was making a direct comparison with the true boolean value. Especially since when the number 1 is passed into the functions that utilize value == true, it outputs true, which means that the functions found 1 == true to be a true statement, as expected.

Thank you for taking the time to answer my questions. I had to do some additional research but found the answer in this article:

Difference Between =, ==, and === in JavaScript [Examples]

It explains that “Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison. So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converted to zero. A string with no numeric value is converted to NaN (Not a Number), which returns false.”

This is why the functions that utilize value == true returned false when the input parameter is ‘hello’, because ‘hello’ is a string without numeric value, so it is always returning NaN == true, which is false.

So using the == operator in the functions, 0 == true is is evaluated as false, ’ ’ == true is evaluated as false, 1 == true is evaluated as true, and ‘anyNon-numericString’ converts to NaN == true which is false.

I didn’t know about the strange conversions that happen behind the scenes when the == operator is utilized.

Bottom line, only use the coercive, == when the inputs are controlled and the outcome is one your program can use (as in, valid).

print (1 == true);
print ("a" == true);
print ([] == true);
print ({} == true);
true
false
false
false
print (1 && true);
print ("a" && true);
print ([] && true);
print ({} && true);
true
true
true
true
2 Likes

Hi - I get the error ‘If an argument that evaluates to falsey is passed to the function, the function should return false.’ The following code does return False in the console, but the error won’t let me proceed to the next assignment. What am I not doing right?
const truthyOrFalsy = (value) => {
if (value) {
return ‘True’;
}
return ‘False’;

}

console.log(truthyOrFalsy(-0)) // Should print false

As an aside, we would never represent boolean primitives as a string.

return true

return false
1 Like

Sorry but that doesn’t help…

How not? It’s what the error message says is expected.

The simplest way I came up with was this:

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

console.log(truthyOrFalsy(0))
console.log(truthyOrFalsy(1))
console.log(truthyOrFalsy(String))
console.log(truthyOrFalsy(Number))
console.log(truthyOrFalsy(undefined))
console.log(truthyOrFalsy(null))
console.log(truthyOrFalsy(NaN))
console.log(truthyOrFalsy(isNaN))

I leave you my solution, I hope it helps you. I was reading the forum to see if someone else solve it differently but I’m not understanding anything here! :exploding_head:

This is my code, if there are other ways to solve this exercise, let me know!

const truthyOrFalsy = (statement) => statement? true:false;

console.log(truthyOrFalsy(0)) // Should print false

HINT (To achieve the cleanest solution to the truthyOrFalsy() exercise)…
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!

There are some expected constructs in this exercise, if I’m not mistaken.

1 Like

This worked for me…

const truthyOrFalsy = val => !!val;

Now for the lay people here, you could support it with an explanation…