FAQ: Conditional Statements - Logical Operators

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) {…
etc…

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.

At line 3 , tirednessLevel murst be equal to 6 in order for your program to work;
Also, line 2 changes the vaiable type to boolean from string and the === sign does not do type coercion .

Hi Guys,

I am also stuck in ! not operator and having hard time understanding the concept. Is there anyone who can explain it with simple example.

The not operator `!` simply toggles a boolean to it’s opposite.

``````console.log(!true); //prints false
console.log(!false); //prints true
//here we assign a variable to the value of an evaluated expression:
const myBool = 15 > 50; //15 is not greater than 50, so myBool will be assigned to false
console.log(myBool); //prints false
console.log(!myBool); //prints true
//we can apply the ! operator at the time of assignment as well:
const myNewBool = !(15 > 50); //the expression inside the parenthesis evaluates to false, and the ! operator toggles it to true
console.log(myNewBool); //prints true
console.log(!myNewBool); //prints false
``````

Thanks , so it’s result is kind of opposite to what the actual value is ?

Kind of, yes. The opposite of `true` is `false`. They are the only two values in the Boolean data type set. NOT is a boolean operator that negates, or toggles the value it is paired up with (operand). When paired with true, the result is false. When paired with false the result is true.

It doesn’t end there, though, just the ‘opposite’, it begins there. NOT can be paired up with any value or expression. This time it negates the boolean evaluation of the expression. To make this evaluation the truthiness is assessed.

`````` value     truthiness (truth value)
0          falsy
""         falsy
null        falsy
undefined     falsy
NaN        falsy
``````

All of these values can be negated, that is toggled to `true`.

``````! 0                  =>  true
``````

Note that the toggled outcome is always a boolean.

``````! 1                  =>  false

! (21 === 42 / 2)    =>  false

! ('a' === 'A')      =>  true

! ('a' == 'A')       =>  false
``````

The NOT operator takes only one operand and has higher precedence that many operators so is often the first expression to be evaluated. That is why above we use grouping to allow the expression in brackets to be evaluated first, then NOT is applied to the outcome.

2 Likes

In the instructions for this exercise I am still getting a Red X and in the Main.js screen I am getting " Did you log `'time to sleep'` ? Not a big deal I think because I can move forward anyway… just wondering why I can’t get the green check mark? I am getting the correct log to the console. Below is the code I have…

Actually, I just figured out that I can’t move forward. Not sure what the problem is here??

No exclamation mark.

mft… you are very quick to reply… thanks for that. Just so I understand: I guess the instructors want us to type exactly what they have written?? In playing around with the exercise, I had originally typed “Go to bed!” instead of “Time for Sleep”… and “Not tired yet” for “Not time for bed”. I wasn’t really watching for the green check mark and my console was printing properly… depending on how I changed the values in the variables or which Logical Operator I used. It was only later that I noticed the red X and that I couldn’t move forward. In the end, text that is logged to the console is at the discretion of the programmer… correct???

By the way, I have a green check mark now after removing the exclamation mark… thanks for that!

1 Like