FAQ: Conditional Statements - Truthy and Falsy

This community-built FAQ covers the “Truthy and Falsy” exercise from the lesson “Conditional Statements”.

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

Web Development

Introduction To JavaScript

FAQs on the exercise Truthy and Falsy

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

let favoritePhrase=[ ];

if (favoritePhrase) {
console.log(“This string doesn’t seem to be empty.”);//prints this log
} else {
console.log(‘This string is definitely empty.’);
}

why empty array consider as a truthy value even it has no values in it?

2 Likes
let favoritePhrase = [];
if (favoritePhrase) // Returns true because favoritePhrase has a value. It's an empty array, but it's still a value.

Individual array elements will return false if they don’t exist.

let favoritePhrase = ['Hi'];
if(favoritePhrase[0]) // Returns true.
if(favoritePhrase[1]) // Returns false.

To check if an array is empty use the length property.

let favoritePhrase = [];
if (favoritePhrase.length === 0) // Returns true for an empty array.
if(favoritePhrase.length != 0) // Returns false for an empty array.
5 Likes

It’s weird, but you need to consider a single space as Truthy, even if there are no letters or numbers.

Not really weird when we consider a space is a printable character, a string with length 1. Only the empty string is evaluated as falsy, all others are truthy.

3 Likes

I should have said “weird for a beginner”. You’re right, a space as a printable character makes perfect sense.

1 Like

Hoping someone can clarify:

Why does this check out to be ‘false’.

let numberOfApples = 0;
if (numberOfApples){
console.log(‘true’)
} else {
console.log(‘false’)
}

while this checks out to be true

let numberOfApples = 0;
if (numberOfApples===0){
console.log(‘true’)
} else {
console.log(‘false’)
}

Thanks in advance!!

Hello, @ajax7383493197.

In your first snippet, numberOfApples has a bool value of false because 0 has been assigned to numberOfApples. In JavaScript 0 is falsy.

In your second snippet, you are doing a comparison. Is numberOfApples equal to 0, or perhaps more appropriately, the value assigned to numberOfApples is equal to 0 (true or false)? The expression is true.

1 Like

The above evaluates the truth value of numberOfApples.

value    truth value
  0         falsy
  n         truthy  (n !== 0)

The above evaluates the expression to yield a boolean, either true or false.

2 Likes

This explanation makes sense. Thank you for clearing this up! :sunglasses:

1 Like

Thank you, this really helped!

1 Like

Here’s a little extra study.

const n = 0
console.log(n == false) //true
console.log(n === false) //false

Ok, let me see if I actually really do have this concept down.

const n=0;

console.log(n==false); returns true because 0 is a false value. Is that accurate?

console.log(n===false); returns false because ‘===’ is strictly for comparing exact values and these values return false because they are not the same type. Please correct me if I’m wrong, but the way I am understanding this is, in this case ‘n’ and ‘0’ are actually two different object types… one is a string, the other, a number. Therefore, false. Am I way off the mark?

1 Like

0 evaluates as a falsy truth value. In the expression, because loose typing is applied, the value is coerced to a boolean to match the other type. Zero is falsy so it gets coerced to, false and the equality is then true.

Loose typing and coercion are a part of JavaScript (ES) that many C and Java programmers complain about. There is good reason to complain, but also good reason to get a handle on loose typing and coercion. It is not a thing to be treated lightly, nor a thing to hate out of the box. It has its uses.

Strict typing, on the other hand has no ambiguities. It is what it is. Type for type. Identity. In truth, this is the least confusing relationship to get to understand.

a === b

if, and only if both a and b have the same datatype; and, a and b are exactly alike.

No confusion there. The loose typing, though. Another question. Loose typing must be purpose driven, and reasonably vetted to be of much production value. Strict typing, on the other hand leaves no question.

5 Likes

The value assigned to n is a number. false is a boolean, so they are not of the same type.

1 Like

undefined, null, NaN … what is the difference between these values?

Consider,

let a;
console.log(a)
//  undefined
let b = prompt("click Cancel or press Esc")
console.log(b === null ? 'null' : b)
//  null
let c = Number('forty-two')
console.log(c)
//  NaN

Ping this topic if more explanation is needed.

so if the user pressed Esc or clicked cancel it will be a null value ?

and here please I didn’t understand why ‘c’ is NaN?

Yes, the function will return null rather than a String object. That’s how we detect it.

NaN is what is returned when we cast a non-number to a Number object. It literally means, Not a Number.

4 Likes

i got it !
thank you :dizzy:

1 Like