FAQ: Conditional Statements - Logical Operators

NaN is not a variable, it is a value, oddly enough of the ‘number’ type.

A string is not a number so will return true from isNaN('string').

I know i am getting annoying at this point sorry in advance but
when does if (myVariable) become a falsy value of NaN nature?
or can you tell me a value that is a NaN?

What is more annoying is me giving you incorrect information. NaN is a variable, in that it is a property of the global object. The names of properties are known as, ‘variables’. D’oh!

Thing about NaN is it is non-writable. We would never, and that is, never assign a value to this variable. It has a value built in: Not a Number.

Be sure that you are comfortable with truthy and falsy, in general terms. Not many things are falsy, so that group will be easy to remember. Anything not in that list is truthy. Easy-peasy.

1 Like

Thank you so much Mr.

1 Like

Extra Study

About NaN

We’ve already learned that NaN is a property of the global object, been advised to never assign a value to it, and also been advised that it is a rare instance that the variable would ever appear in production code.

JS has a built in function, isNaN() that can evaluate any object and determine if it can be coerced to a ‘number’ type. If it cannot be coerced, the return is, true.

That would be the expected outcome if we tried it on, 'string' The function is testing for a possible outcome that is numeric.

Now we turn to the Number method, .isNaN() which is not the same function as above. It is looking specifically for, NaN. Note the difference and review this often until both function and method are clear in their usage.

Operations such as parseInt() and parseFloat() will return NaN when they cannot successfully resolve their inputs. Same applies to Number() and unary, + instances that cannot be resolved. JS returns NaN. We then test the outputs of these operations wherein lies the ubiquitous, NaN.

Yes, learn about NaN in all its intricacies. Don’t read too much into it though. Let the real stuff fill that void.

1 Like

Will do so thanks again :pray:t5:

1 Like

Didn’t mean to delete previous post. I have updated the code. Get the right answer but still can’t advance. What am I doing wrong?

let mood = ‘sleepy’;

let tirednessLevel = 6;

if ((‘mood’ === ‘sleepy’) && (‘tirednessLevel’ > 8)) {

console.log(‘time to sleep’);

} else {

console.log(‘not bed time yet’);

}

mood should be an identifier, not a string.

1 Like

Thank you. I was going insane.

1 Like

Hello! I have a question regarding the NOT operator. Does the position of the ! operator matter in a condition statement?

let mood = ‘sleepy’;
let tirednessLevel = 6;
if (tirednessLevel > !8 && mood === ‘sleepy’) {
console.log(‘time to sleep’);
} else {
console.log(‘not bed time yet’);
};

during this exercise, I noticed that the value printed changes when I change the location of the “!” operator in the third line of this code, which is : if ( tirednessLevel > !8 && mood === ‘sleepy’). When I type this, it indicates ‘time to sleep’, but when ii change the"!" operator to before the condition like if ( !tirednessLevel > 8 && mood === ‘sleepy’), the value changes to ‘not bed time yet’.
Someone pls help me understand this I am so confused !

let mood = ‘sleepy’;

let tirednessLevel = 6;

if (!mood === ‘sleepy’ || !tirednessLevel > 8){

console.log(‘time to sleep’);

} else {

console.log(‘not bed time yet’);

}

mood === sleepy will return true however, bang operator will mean it comes out as false.
tirednesslevel > 8 will return false however, bang operator will mean that it comes out as true.

We have an if statement that contains or, so we have false || true.

Once condition is true, so the if should run.

Why is my else running instead?

By playing around with the NOT operator, I noticed weird interactions between boolean and number conditions:

let tirednessLevel = 6;

if (!tirednessLevel<7){
  console.log('true');
} else {
  console.log('false');
} 
//prints true

but

let tirednessLevel = 6;

if (tirednessLevel<!7){
  console.log('true');
} else {
  console.log('false');
} 
//prints false

so I guess that number per default is true in JS…
but “false<7” is true and “6<false” is false?
I don’t understand this interaction :sweat_smile:
Can someone more experienced maybe explain a little more what is happening?

Hi! I am just starting to code but…

You use a “=” and I think, this way you are declaring immediately that the statement is true.

So in the if … else . If true do the first choice, ELSE do the second choice.

But if you used the “===”, you would compare the condition - “if (mood === true)” with the condition of this situation, you declared in line 2, that “mood = false”.

Here the program will do a check if the condition “if (mood===true)” is equal to statement in line 2 “mood =false”, in this comparison the program will see that this situation is “false”, because this two are different.

Hello there!
Does using logical equivalence of statements make the code more efficient? I understand that doing that scarifies code readability, but what about code efficiency? For example

let day = 'Friday' let workConditionNotMet = (day === 'Saturday' || day === 'Sunday'); (workConditionNotMet && console.log('Enjoy the weekend')) ^ (!workConditionNotMet && console.log('Go to work!'));

Where this is equivalent of a simple if and else statement.

let day = 'Friday' let workConditionNotMet = (day === 'Saturday' || day === 'Sunday'); if (workConditionNotMet){ console.log("Enjoy the weekend");} else { console.log("Go to work"); }

Will that increase the performance and efficiency of the program, the same as minifying?

(workConditionNotMet && console.log('Enjoy the weekend'))

        expression              statement

The above is an expression that contains a statement, which is non-standard use of a logical expression. We can see the logging since JS will execute that statement, first, then evaluate the expression afterward, which will always be false since console.log() returns undefined.

An if statement is preferable to such irregular usage, or even a ternary:

           expression       expression         expression
console.log(condition ? "Enjoy the weekend" : "Go to work!")
            ------------------Expression-------------------
[                        STATEMENT                          ]
1 Like

what is the different between && and &, I used & it gave me the same result as &&

They are far from being equivalent operators given than one, && is a logical operator and the other, & is a bitwise operator. Yes, they are both ‘AND’ but in no other way are they the same. Consider:

 > 5 && 15
<- 15
 > 5 & 15
<- 5

The outcomes are different because the operations are different.

5 && 15 tests for co-truthyness, and if found, yields the last operand’s value. What makes them both truthy is that they are non-zero.

5 && 0  =>  0
0 && 5  =>  0

Zero is falsy and short-circuits the operation and yields the falsy operand. Bottom line, there is logic being applied, here.

5 & 15 is a bit comparison operation.

0b1111 & 0b0101  =>  5

It’s 5, (0b101 in binary) because those are the bits that match. The 2^2 bit and the 2^0 bit are set in both numbers.

0b1010 & 0b0101  =>  0

None of the corresponding bits are both set so the yield is, 0.

Until you get to the unit that does bitwise operations such as above, among others such as masking, shifting, etc. it would do well to stay away from this operator in your code logic. The && operator is the one you will be using at all times.

1 Like

Instead of seeing the entire code block as a whole, you can try to breakdown each section of your code into smaller bits and see how are they displayed in the console.

let mood = ‘sleepy’;
let tirednessLevel = 6;
if (tirednessLevel > !8 && mood === ‘sleepy’) {
console.log(‘time to sleep’);
} else {
console.log(‘not bed time yet’);
};

Case 1: if (tirednessLevel > !8 && mood === 'sleepy')

In this case, the ! operator is applied to the number 8. So, you can first write

console.log(!8) // false

The logical NOT operator inverts the truthiness of a value. In JavaScript, the logical NOT operator applied to a non-zero number evaluates to false. Since !8 evaluates to false, the comparison becomes tirednessLevel > false, which is essentially equivalent to tirednessLevel > 0.

This means the condition is true when tirednessLevel is greater than 0.

console.log(tirednessLevel > !8) // true

We now know that the first condition of the conditional statement is true, as for the second part:

let mood = ‘sleepy’;
console.log(mood === ‘sleepy’) // true

Since both conditions result in a truthy value, the message time to sleep is displayed to the console.

Case 2: if (!tirednessLevel > 8 && mood === 'sleepy')

In this case, the logical NOT operator is applied to tirednessLevel. As such:

let tirednessLevel = 6;
console.log(!tirednessLevel)

If tirednessLevel is a non-zero number, applying ! to it will result in false. Therefore, the condition becomes false > 8, which is always false. Since, false is coerced to 0 and 0 > 8 is obviously wrong. So, the condition is false, and the code in the else block is executed, resulting not bed time yet being displayed to the console.

Read this post A.K.A post above for some insights