0

I'm new in Js and i just want to know if my idea of the ! (NOT operator) is right.

e.g

const hasDriversLicense = true;
const hasGoodVision = true;
const isTired = false;

if (hasDriversLicense && hasGoodVision && !isTired) {
  console.log("Sarah is able to drive!");
} else {
  console.log("Someone else should drive..");
}

in the previous code the condition is that if...

  • Sarah has driver's license, and
  • if has good vision, and
  • is NOT tired

she should be able to drive, okay?

The way I was analyzing this in code was well I have a hasDriversLicense var which is true so this means that Sarah actually has a drivers license, and in relation of this way of thinking the next condition also make sense, so hasGoodVision is true so it means it has a good vision.

My confusion comes when we are in this part !isTired, right now the isTired var is set it to false that means that if we relate this with our if condition Sarah is able to drive.

So when we say !isTired that means that isTired is false and then becomes true cause of the ! so this means that actually Sarah is Tired? and she is not able to drive

This way of thinking was confusing but I start thinking that the ! works as a validator not really as a value changer of the complete context. What I mean with this is that we can say that this !isTired is like saying "validate if this var value is actually false in that case return true", so it's true that is false that's why we get this. Is this a valid way see the behavior of this operator?

want to know if my idea of the ! (NOT operator) is right.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    There's a lot going on here and I'm not sure if any of it makes sense, but it does sound like a trip to [this Wikipedia page](https://en.wikipedia.org/wiki/Boolean_algebra) is in order, or at least a crash course on boolean logic. – tadman Jan 24 '23 at 02:01
  • Tip: Test in the JavaScript console `!true` and `!false` and `!!true` etc. – tadman Jan 24 '23 at 02:03
  • 1
    _"this means that actually Sarah is Tired?"_... Simply read `!isTired` as _"not (is) tired"_ and it should make sense – Phil Jan 24 '23 at 02:05
  • _"so this means that actually Sarah is Tired?"_ - if `!isTired` is `true` then it means Sarah is not tired. – evolutionxbox Jan 24 '23 at 02:10

1 Answers1

0

I think you got it right yea

const isTired = false

console.log(!isTired) // output: true

"!" means "NOT" in my mind

Like you said it won't change the value but validate and check whether it's, in that case, "not" truthy or "not" falsy

Max S.
  • 81
  • 4