FAQ: Code Challenges: JavaScript Fundamentals - truthyOrFalsy()

#1

This community-built FAQ covers the “truthyOrFalsy()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise truthyOrFalsy()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

what is wrong with this code? it returns undefined. However the solution offered can also be built up/converted like this with ternary operator.

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

#4

What the heck? I set my parameter to (value)
I then set an if statement for if value <= 0 it returns false, or if value >=1 it returns true.

This works just fine, and was working as expected. You can get this same result a few different ways. I had to press the solution button for the first time, and it wasn’t needed because it wasn’t necessarily a wrong answer. They didn’t mention to perform ________ to the javascript in the example.

#5

How terribly disssappointing it must be to arrive at a solution that satisfies you, and not the lesson checker. Clearly you are missing the point of the exercise. There is no 0 or 1 in the question. Those are literals. We are dealing with truthy and falsy. That’s a different can of worms, and not to be taken literally.

1 && true

What should be the outcome of that expression?

var a
a && true

What should be the outcome of that expression?

a || 0

What should be the outcome of that expression?

#6

I came up with the same solution, and it worked fine.

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

I guess, return matters in this case. But why? Anybody?

1 Like
#7

A return statement that is a boolean expression will be evaluated, then that boolean is returned.

function truthyOrFalsy(value) {
    return value * 7 === 42;
}
console.log(truthyOrFalsy(7))    // false
console.log(truthyOrFalsy(6))    // true

We never really have to return a literal so long as the return value is a boolean expression.


 > function truthyOrFalsy(value) {
       if (value.toString() === 'true' || value.toString() === 'false') {
           return value;
       }
       return value ? 'truthy' : 'falsy';
   }
<- undefined
 > truthyOrFalsy(true)
<- true
 > truthyOrFalsy(false)
<- false
 > truthyOrFalsy(1)
<- "truthy"
 > truthyOrFalsy(0)
<- "falsy"
#8

I tried the following code, when I run it it gives me true and flase output, but the exercise is still not correct. What did I do wrong?

function truthyOrFalsy(input) {
if (input === undefined || input === false || input === 0 || input === NaN || input === null || input === ‘’) {
return false;
} else {
return true;
}

1 Like
#12

This works for me:

const truthyOrFalsy = (value) =>{return (value ? true : false);}

#13

here i fixed ur code!
const truthyOrFalsy = (anyValue) => {
anyValue = anyValue ? true: false;
return anyValue
}
the problem was is that is wasnt returning anything and since you used a ternal operator you cant add that there.
i simply solved it by making the value of “anyValue” to the same ternal operator you used and then returning it!

#14

Hello what is wrong with this code?

const truthyOrFalsy= (value) => {
  if (value === false || value === null || value === undefined || value === 0 || value === NaN || value === '' || value === "" || value === `` || value === document.all ) {
    return false;
  } else {
    return true;
  }
}

I don’t get errors and get the required result, however, I cannot continue with the exercise as it counts it as wrong. Is it because the code is too long compared to the desired one such as
const truthyOrFalsy = value => { if (value) { return true } return false } ?

#15

Any falsy value will cast to false in a conditional. It will also cast to false in a boolean expression if we double not it.

return ! ! value;

That means we never have to explicitly compare it to falsy values, just test the value itself for truthiness.

 > truthyOrFalsy = value => !! value;
<- value => !! value
 > truthyOrFalsy(0)
<- false
 > truthyOrFalsy('')
<- false
 > truthyOrFalsy("")
<- false
 > truthyOrFalsy(null)
<- false
 > truthyOrFalsy(undefined)
<- false
 > truthyOrFalsy(NaN)
<- false
 > truthyOrFalsy(document.all)
<- false

As we can see, the recommended solution you posted does test the value for truthiness and return accordingly. We’ve seen this expressed also as a ternary

return value ? true : false;
1 Like
#16

The same question here…I wrote it this way:

function truthyOrFalsy(value) {
if (value === 0 || value === undefined || value === ‘’ || value === NaN || value === null || value === false) {
return false
} else {
return true
}
}