FAQ: Conditional Statements - Logical Operators

This community-built FAQ covers the “Logical Operators” 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 Logical Operators

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!

31 posts were split to a new topic: Understanding the NOT operator!

3 posts were split to a new topic: When should I use optional parenthesis?

21 posts were split to a new topic: Maximum function challenge

6 posts were split to a new topic: How can I type the OR operator || (also called pipe)

I think many people can get confused as to whether why their code is wrong, in this case, I got confused thinking about what error I had possible made while writing it. The problem:

If both conditions evaluate to true, then console.log() the string 'time to sleep'

So me, being a grammar nazi wrote:

Time to sleep.

The same happened with:

Otherwise, we should console.log() 'not bed time yet'.

It’s not something THAT big of a deal, but I think you guys could at least make both versions (The one with lower case and the one with upper case) pass, no matter what.

3 Likes

3 posts were split to a new topic: Missing preset code?

3 posts were split to a new topic: What is the difference between = and ===?

9 posts were split to a new topic: How to set a variable as a conditional

7 posts were split to a new topic: How to calculate during string interpolation?

The tutorial needs to be checked for syntax. There are various points in which we are not putting semicolons in our conditional statements, then an example will show one in the conditional. Then there is an example where they put a semi colon after an else statement’s closing curly bracket.

Hi guys! I was playing around with the logical operators and given this:

let mood = 'sleepy';
let tirednessLevel = 6;

if (!mood === 'sleepy' || !tirednessLevel<=6) {
  console.log("time to sleep");
} else {
  console.log('not bed time yet');
}

why is it returning ‘time to sleep’ and not “not bed time yet”?

I am going wrong somewhere, but not sure where.

Thanks!
Ilaria

The first branch will only be followed when the conditional yields true. Consider,

mood === 'sleepy'    =>  true
tirednessLevel <= 6  =>  true

true || true         =>  true
! true || ! true     =>  false
1 Like

Tried to concatenate boolean

true + true outputs as 2
false + false outputs as 0

seems like true = 1 and false = 0

true + ‘true’ outputs as truetrue, so it changes data type while concatenating

i guess my question is what rules apply to this? was really expecting it to output true + true = true

What you are witnessing is known as type coercion.

true + true

is coerced to its simplest equivalent, an integer. 1 is truthy so the above is coerced to,

1 + 1

In your second example,

true + 'true'

the string, 'true' cannot be coerced to a number, so the boolean is coerced to a string, hence,

'true' + 'true'

The above does not apply in strict mode. Coercion is a process permitted by loose types.

true == 1   =>  true    // coercion of loose type
true === 1  =>  false   // strict comparison so no coercion
1 Like

I’m sorry to drag you back sir, but I still don’t quite understand. Since the if block came back as false, then the else string (‘not bed time yet’) should have been executed, but it didn’t. How come?

Please show us your code.

I don’t know how to share my own code yet, but it’s the same with what this fellow user posted.

Question! Why, in the experiment I did here, does the conditional evaluate to true even though I already changed it to false?

Hi everyone, I am trying to work through this section. Here is what I wrote out:

I am not sure where I am writing my code out incorrectly. Any results with a detailed explanation for this noob would be great! Thank you in advance.