Codecademy Forums

Understanding the NOT operator!

I think I’ve seen the light! :sun_behind_small_cloud::smiley:

Working through all of your examples with numbers, and with both AND (&&) and OR (||) I can see that the 2 ‘facts’ that I’ve stated, together with the four examples, in my previous post are correct. I came at it from the angle of an if(statement) with comparison expressions, because that was what was used in the exercise which first triggered my questions. Thanks for getting me to also come to the same conclusion from the numbers angle - that helps to prove it. :+1:

I can also see how we can use the same examples to prove De Morgan’s Laws, which @chrisgallegos mentioned. I’ll probably experiment with that a bit more.

I’m going to wait until I’ve covered Functions before attempting this! I know when to pause and take stock, before trying to bite off more than I can chew (for the time being at least)! :wink:

1 Like

Thanks stetim94, was going to ask same question. That means we would have to remove the variable when using the ! operator in our console.log();

let excited = true;
let mood = 99;
if (! true || ! 99){
console.log(‘Time for break’);
} else {
console.log(‘Break is over’);
}

That expression will always yield false.

if (! excited || ! mood)

! excited will be false if excited is true. ! mood will be false if mood is non-zero.

1 Like

Hello.
I am having problems grasping the idea of the NOT operator !

The basic idea, as seen in the lesson, is that the NOT operator turns True into False and False into True, so I was practicing with the code in the lesson and made this:

let mood = ‘sleepy’;
let tirednessLevel = 8;

// Here I am checking if mood equals to ‘sleepy’, which is True, so that condition is met. Then I check if tirednessLevel is greater than 10, which for the value we have is not greater, so that would be False, but since I am using the Not operator I understand that instead it turns into True, so then, my conditional would be met and the output in the console would be: time to sleep. But instead of that, it goes into the else statement. Why is that?

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

Hello, @lslozano

What would the opposite of tirednessLevel be? What is not 8? To do what you are attempting, you’ll need to move the ! operator to apply to the evaluated expression not just the numeric variable tirednessLevel.

2 Likes

Hi! @midlindner Got it!

I moved the ! operator outside the evaluated expression so it applies to all of it.
It ended up like this:

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

This evaluates the condition and as it is, returns a false but by applying the Not operator to it, the returning value will be true.

Thanks a lot!

2 Likes

Excellent! Just remember that the ! (not) operator can only be applied to boolean values. Those values could be assigned to variables, literal bool values (ie. true, false), or any expression that evaluates to a bool value as was the case with your experiment. Of course there is also the != (not equal) operator that can be used in comparing two values. Happy coding!

2 Likes

Your answer and follow up was very helpfull, this has given me new perspective to better understand the ! (not) operator and as a bonus the != (not equal) operator.

I appreciate the help. See you around!

1 Like

I would argue that NOT can be applied to any value or expression, regardless whether boolean or not.

! ""              =>  true
! 'a'             =>  false
! function () {}  =>  false
! NaN             =>  true
! 0               =>  true
! null            =>  true
! []              =>  false
! {}              =>  false
! 1               =>  false
! undefined       =>  true

That operator is coercive

1 != '1'          =>  false
1 !== '1'         =>  true
1 Like

I believe we are discussing semantics. You are applying the ! operator to the boolean value of each expression. I imagine when we place the ! operator before an expression such as 'a' that 'a' is implicitly converted to it’s bool value, and then ! is applied.

console.log(Boolean('a')) // true
console.log(!'a') // false
2 Likes

Semantics, perhaps, but it narrows down what NOT actually does. It first coerces a boolean from the value, then negates it. I think it is important to see that distinction as opposed to saying it can only be applied to booleans.

2 Likes

Are you on the Republic App also?

Not even sure what that is, so, no, not on Republic App.

when we apply a logic it is also a good thing to break computation steps down

if (!(x === ‘blue’) && !(y > 5))
1. is x identical to 'blue'? //yes, then is TRUE
2. make it not. //so now is FALSE
3. && let me see if the first is TRUE, ouuu is FALSE // so forget everything else and return FALSE
4. Result = FALSE

if (!(x === ‘blue’ && y > 5))
1. is x identical to 'blue'? //yes, then is TRUE
2. && let me see if the first is TRUE, ok is TRUE // let's move further
3. is y greater than 5 , no it is FALSE // so all the result is FALSE 
4. now let's make it not // now is TRUE
5. Result = TRUE

I hope my way of thinking helps
it also may prevent making mistakes like

x === !8 // is x identical to not 8 ... hmmmmm

tell me if it makes sense :sweat_smile:

1 Like

Yes, that is well explained, and I like how you break it down into logical steps :smiley:

Shouldn’t that be:
// is 'x' identical to 'false' ?
Wouldn’t the logical steps here be as follows?

  1. The NOT operator (!) coerces the value 8 to the boolean true, and then negates it to false.
  2. Is the variable 'x' identical to the boolean false? ------> No, because 'x' is identical to the string 'blue'.
  3. Result = FALSE

Unlikely that this use of the NOT operator would ever find a practical use, but theoretically it can still be broken down into logical steps. And I think that’s precisely your point: the logical steps here showing that it most probably isn’t doing what you intended, and is therefore a mistake. :wink:

1 Like

That is nice :hugs:, you got my idea :heart_eyes:

1 Like