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

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.


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!


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!


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

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.


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

Hi @stetim94 I’ve just started learning JS and thought it would be helpful for other novices (like myself) to point out that this isn’t correct. As @chrisgallegos kindly explained, these two expressions are not the same.

So, to negate the entire statement, you would have to structure your code as per your second expression:

Hope this helps everyone :slight_smile:

I am also trying to understand the NOT operator but I am not sure when this will be used, or in what scenario do we use this? Do I add it on my finished script once I realised that a certain line should equal to false?

I am not sure if I am asking it correctly. I am really new to all this programming stuff.

Lets say i have an object which has a number of properties, and based on 4 or 5 properties i need to determine if the object is “valid/active”, so i wrote a function for it.

but then later i also needed to check if a object was invalid/inactive, so i simply did:

if (!object.isActive())

otherwise i had to implement yet another complicated method

even if i wanted an isInactive method, i would simple call isActive within the isInactive method and use the not operator, this way, the business logic is at one place.

it can even happen that the method in question is in some package written by someone else, so you can’t just modify simply modify it.

the NOT operator is certainly useful. You will hopefully see this with time :slight_smile:

1 Like

I understand it now. Thanks for the explanation! :grinning::grinning::grinning: