Understanding the NOT operator!

I actually don’t understand it yet cause there was so many comments. anyone can actually just write the right answer from that example from the exercise using the ! operator. will be much easier that way. thank you

understand what? What the not operator is? Why you would is the not operator?

well, in the example when they explain it they used it that way
let excited = true;
console.log(!excited); // Prints false

let sleepy = false;
console.log(!sleepy); // Prints true
But in the exercise the only way I see it doable is like this
let mood = ‘sleepy’;
let tirednessLevel = 8;

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

As you can see I have used the ! inside the if declaration and not in the console log as they showed in the example cause every time I tried to use it inside the console log bracket I get an error

Comparing to a boolean is redundant, but we do have an operator for this situation…

if (mood !== 'sleepy' ...) {}
1 Like

I know it does not add to the topic but you have to be the best user on the entire site, always helping! Your contribution constantly aids me when I’m stuck. Keep it up man!

4 Likes

Would you mind illustrating this in some sort of basic “real world” setting?
Meaning, could you write some simple code where this could have a “real world” application?

Thanks!
-jS

Not (pun unintended) really. Until we immerse ourselves in boolean and circuit logic, real world examples are as difficult to explain as quantum physics or black holes.

NOT is such a powerful concept that the computer age grew out of it. We would never have discovered its potential without the help of some incredibly gifted people of their day…

History of logic - Wikipedia

That’s the basis of our real world setting. Now consider,

A NOT B

In spite of all its inner workings, logic is something we possess, not to ponder, but to explore.

Don’t even start with Probability.

Indeed, Sextus says “According to Philo, there are three ways in which a conditional may be true, and one in which it may be false.” Philo’s criterion of truth is what would now be called a truth-functional definition of “if … then”; it is the definition used in modern logic.
ibid

P(E) = A NOT B

Electronics, quantum theory, the list goes on.


NOT implies either an AND or an OR.

A AND NOT B

A OR NOT B

We could read A NOT B as either, but the general interpretation is AND.

A AND NOT B

NOT B can be given a label, let’s call it K.

A AND K

The outcome will be false, three out of four times.

A and K must both exist.

2 Likes

Why do I need to use the bang operator if I can just set it to true or false so if excited true then turn false but just why

It’s only called the bang operator in Ruby, afaik. The ! (exclamation mark) is the NOT operator in JavaScript.

When our code is running we cannot set values. All setting of values is done by the program, as is all evaluation of states to determine control flow and other logic.

yes I understand that when its running you cant change it but I could put happy=false before

I know that working for the website is hard because SO MANY PEOPLE but please have a answer tomorrow

Perhaps think of rewording your question so we can understand what you are asking?

never mind I found out why but thx

1 Like
let mood = 'sleepy';

let tirednessLevel = 6;

if (mood === 'sleepy' && !(tirednessLevel > 8)) {

console.log('time to sleep');

} else {

console.log('not bed time yet');

}

I’v been messing around with this code as is suggested once I’d completed the task. One thing I didn’t understand is why I have to put tirednessLevel in brackets (see above) to get the NOT operator to reverse it to true. However, when I want to use the NOT operator on mood (eg. !mood === ‘sleepy’) I didn’t have to apply parentheses to reverse it.

Any ideas? Just out of curiosity.

Whereas

order of operations:

Operator precedence - JavaScript | MDN

that looks weird, then i would just use !==. When you would use a linter, which check coding style, doing !mood === 'sleepy' does give a warning, because now the condition can never be true

Not applies to the expression to its immediate right. It sees tirednessLevel as that expression, not the comparison, even though it, too, is an expression.

This is not really related to operator precedence, as we can see, but not tirednessLevel > 8 will always be false regardless what the value of tirednessLevel since once NOT is applied, it will become a boolean, and can only be coerced to 0 or 1 in the comparison (type coercion).

By wrapping the comparison expression in brackets, we modify the order of operations.

1 Like

A belated thank you. This all makes sense! :slight_smile:

1 Like

With the && operator both conditions equate to true. Thus the if statement execute. My question(s), why when i put the (!) operator in the else statement does it make the if statement execute value false? I thought the else statement only execute if the conditions of the if statement both negate to false?

Can we see the different codes you used? Based on description alone, we would have to do a lot of guessing.

if you do:

if (){}
if(){}
else {}

the second if and else belong together. The first if might be true, but it doesn’t matter for else.

Hey, thanks for the reply. I’m sorry i just re-read the problem and saw where i was sticking at.