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?