FAQ: Code Challenges: JavaScript Fundamentals - truthyOrFalsy()

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!

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 ;

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.

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?

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

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"

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;


This works for me:

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

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!

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 } ?

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

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

The solution provided is:

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

Why does this code work without the semi-colons after the return statements? The Functions lession #6 “Return” taught me to write:

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

We are taught to end all statements with semi-colons that when written on the same line will collide with each other and corrupt the statements.

a = 6
b = 7

If we collapse them into a single line, we will have,

a = 6 b = 7

which will raise an error since the parser doesn’t stop at the 6 like it should.

a = 6; b = 7

will not raise an error.

There are some relaxed rules, which would apply above. One such rule is that if a code block contains only a single statement, then it doesn’t need to be terminated with ;.

The same applies to the last statement in a code block. It too can be written without a terminator.

As far as semi-colons are concerned, they are a part of the syntax, albeit in a relaxed way so we really should form the habit of using them as dictated. Other languages use the same end-of-statement syntax and they are not so relaxed. If you have this habit formed already, you’ll run into less errors in those languages.


among others, I’d suspect.


I found this to be the simplest solution, even simpler than the solution that is provided in the excersise:

const truthyOrFalsy = (value) => value ? true:false

I am confused how does the function return true or false without the “value” being defined?

Do you mean how does it return true or false without actually returning a literal?

sky = 'gray'
console.log(sky === 'blue')    // false
console.log(sky === 'gray')    // true

Expressions have a value; comparison expressions evaluate to a boolean value.

Truthy means an expression yields true when evaluated. Falsy means an expression yields false when evaluated.

The simplest form of expression is a value literal


All of the above are expressions that are falsy.


All of the above are expressions that are truthy.

if ([]) {
    console.log("Array expressions are truthy")
// Array expressions are truthy
1 Like

It seems that the function declaration demands an explicit return. The ternary operator has already an implicit one. Is that it?

I thought that doing this way, you’d have to specify all the possible falsy values in Js. For example, ![ plus ] (wihthout the word “plus”) is falsy (https://guide.freecodecamp.org/javascript/falsy-values/). But your code worked fine, returning “false” as expected. So, I don’t have the answer too, for why codecademy exercise didn’t accepted your code…

I had my own dissappointment… my code didn’t work:

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

but it returns “false” with any value different from 1. Why is that so?

The parentheses are not necessary, as the alternative solution provided by the exercise shows:
// Using a ternary:

const truthyOrFalsy = value => value ? true : false

a slightly simpler solution!