FAQ: Conditional Statements - Logical Operators

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

4 Likes

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

Just trying a new tact, this is only my sixth day in codecademy-- or working with code for that matter. Thought I would share how I am trying to learn, incase others are having a hard time too-- hope it will help someone… or me with a helpful reply. lol

let mood = ‘sleepy’;
let tirednessLevel = 6;
if (mood === ‘sleepy’ || tirednessLevel > 8) {
console.log(‘time to sleep’)
}else {
console.log(‘not bed time yet’)
}
let thirtyRdMag = ‘check out’;
let california = 1;// (1)not appoved for high cap mags;
let texas = 1;// (1)not appoved for high cap mags;
let florida = 2;// (2)aproved state for high cap mags;
if (thirtyRdMag === ‘check out’ && california === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};
if (thirtyRdMag === ‘check out’ && texas === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};
if (thirtyRdMag === ‘check out’ && florida === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};

time to sleep
does not ship to state!
does not ship to state!
proceed to shipping

For || and && is the left or right side evaluated first? And is the second condition even evaluated if the first one was already true for ||, or false for &&?

Yes, in that order, (left to right) once precedence is determined.

()  =>  brackets
!   =>  not
&&  =>  and
||  =>  or

Operands after a short-circuit are not evaluated since it is moot.

1 Like

What about if the x && y(…) and y has side effects. in java you use single & and it will not short circuit if x is false