FAQ: Code Challenges: JavaScript Fundamentals - truthyOrFalsy()

After trying all sorts of convoluted crap, like pretty much all the incorrect solutions I’ve seen here, I found this via google and it works.

const truthyOrFalsy = value => { if (value) { return true } else if (!value) { return false } } console.log(truthyOrFalsy(0));

Truthy/Falsy seems so simple but it’s the hardest thing to grasp so far.

I am grateful for this exercise in particular because it gave me a reason to hunt down (and subsequently read up on) this way of writing arrow functions which I recalled seeing earlier but couldn’t remember the specifics. Posted here for anyone wanting a reminder - I think this was covered here in an earlier lesson?

Spoiler blur for those who want to work up to it.

const truthyOrFalsy = val => val ? true : false; console.log(truthyOrFalsy(0))

I’m looking for more clerification outside the MDN for [[[IsHTMLDDA]] internal slot]. Is this a physical device?

I don’t really understand where or how this is used. I know it’s probably outside the scope of the course, but if anyone can shine some light on the topic I’d be greatly appreciative.

Cheers,
Trevor

Why does this code not pass? It works.

// Write your function here:
function truthyOrFalsy (value){
if (value === true){
return true;
} else {
return false;
}
}

// Uncomment the line below when you’re ready to try out your function
console.log(truthyOrFalsy(0)) // Should print false

// We encourage you to add more function calls of your own to test your code!

That comparison is explicitly looking for the literal, true which will not match value unless it is actually, true.

We’re testing for truthiness, which means the evaluation is true.

if (value)

will follow the first branch if value is truthy.

1 Like

The following does return the correct answer but is marked as incorrect.

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

I opted for their proposed expanded solution so i could proceed. has anyone experienced similar issues as this?

That is a loose comparison so will accept a 1 as being equivalent.

console.log(1 == true)
<- true

When making comparisons, we should regard type as important as value.

console.log(1 === true)
<- false

The other issue is that above is not testing for truthiness, but equality. To test for truthy values, do not compare to the literal value, true. Use truth value assessment, instead,

val => val ? 'truthy' : 'falsy';

Now all truthy values will be routed through the first branch, and all others through the second branch.

const truthyOrFalsy = val => val ? 'truthy' : 'falsy';

console.log(`[]  ${truthyOrFalsy([])}`)
console.log(`{}  ${truthyOrFalsy({})}`)
console.log(`0   ${truthyOrFalsy(0)}`)
console.log(`42  ${truthyOrFalsy(42)}`)
console.log(`''  ${truthyOrFalsy('')}`)
console.log(`'a' ${truthyOrFalsy('a')}`)
[] truthy
{} truthy
0 falsy
42 truthy
'' falsy
'a' truthy
1 Like

Hi team, I’ve used the solution below, what do you think? Is there a downside to having it shortened this much?

const truthyOrFalsy = arg => arg ? true : false

Thanks! :blush:

Can anyone help me understand why this code isn’t working?

It’s returning the correct values but not accepting it as the solution to the challenge. I’m afraid I’m not seeing something.

function truthyOrFalsy(input) {

if (input === false) {

return false

} else {
return true
}
}

Welcome to the forums! What if we called truthyOrFalsy(0)? We’d expect false to be returned, but instead we get true. Do you spot the error in your code?

Hint: Note that input === false will only be true when input has exactly the value of false. Therefore, your code is currently only checking if something is false, not if something is falsy.

Thank you so much. I see it now.
I had it in a way that a falsy value would evaluate to false on my check of whether or not it was falsy. I totally understand now.

1 Like

Hello I have a simple question. In this exercise, when you click on “View Solution”, it gives the following three codes as the three possible solutions:

1.) The fat arrow solution (which doesn’t use an “else” keyword at the end) :

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

2.) Function declaration (which uses an “else” keyword at the end) :

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

3.) Using a ternary :

const truthyOrFalsy = value => value ? true : false

Here’s where I am confused. I completely understand the number 2 and number 3 solutions above. And if I rewrite number 1 to have an “else” keyword at the beginning of the last line, it still works. My question is, are the “else” keywords even necessary at the end? And if not, is this keyword only necessary in functions where there are MORE than 2 conditions? Or is it only not required for boolean data-type conditionals, such as this exercise? Or we just don’t need an “else” keyword, no matter how many conditions there are? Or no matter the data type?

I ask this, because the Javascript lesson does not mention anything about the “else” statement being optional, or in what conditions we could leave it out. So suddenly when I see this exercise, it shows that it is actually optional. What conditions made it optional? When is “else” needed at the end, and when is it not?

Thank you for your explanation! :slight_smile:

Not when the branches each contain, return. The first example is adequate in that case. The second return is never reached if the first branch is followed.

Were the branches simply actions with no return the else would be necessary.

Again, that would depend whether the branches return, or not.

...{
  if (...) {
    return ...
  }
  if (...) {
    return ...
  }
  return ...
}

We see that subsequent if statements will do the job we want. Take away the return and we would need to write,

...{
  if (...) {
    ...
  }
  else if (...) {
    ...
  }
  else {
    ...
  }
}

Thank you! So the else is only necessary if it’s simply an action without a return. Got it! I hope Codecademy can put this in their javascript lesson! :slight_smile:

1 Like
function truthyOrFalsy(anyValue) {
  return (anyValue) ? true : false;
}

console.log(truthyOrFalsy(0));

Have to return something, I guess. This one’s working.

I’ve done it with ternary s well.

function truthyOrFalsy(anyValue) {
  return (anyValue) ? true : false;
}

console.log(truthyOrFalsy(0));

Hey folks,
What’s the difference between
if (value) and if (value === true) ?

The first one looks only at the truth value; is value truthy, or not?

The second one looks for identity, meaning, is value exactly equal to true, or not.

1 Like
  if (
  value === false || 
  value === 0 ||
  value === -0 ||
  value === "" ||
  value === null ||
  value === undefined ||
  value === NaN ) {
    return false;
  } else {
    return true;
  }

I think this should be accepted :sweat_smile: