FAQ: Conditional Statements - Logical Operators

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.


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.

When we declare a variable with var, let or const the interpreter is expecting one of two things:

  1. a semi-colon; the variable is declared but not defined
  2. an assignment operator (=); variable is declared and defined

In the latter case we would be assigning a value.

let a = 6

I see and, or, and not. Easy enough to follow. Is there a logical operator for “exclusive or”?

Only in the Bitwise operators, ^.

1 Like

Hi guys!! I wanted to give you a resource that I found really useful when learning C++ and also works just the same is JS. Just for you to make more sense of these logical operators:

0 - false
1 - true

I’m wondering if it is possible to adopt logical operators more than once in a sentence. Something like:

if (a || b || c ||d) {…

or even…

if (a || (b && c) || d) {…,

if (a && b &&c && d) {…

Not sure it makes some sense at all… thanks!

All of the above is valid, but we should always be looking for ways to make the logic as unconvoluted as possible. Be sure that precedence is well understood, as well. We should also be aware of short-circuiting.