All of the above is valid, but we should always be looking for ways to make the logic as unconvoluted as possible. Be sure that precedence is well understood, as well. We should also be aware of short-circuiting.
The not operator ! simply toggles a boolean to it’s opposite.
console.log(!true); //prints false
console.log(!false); //prints true
//here we assign a variable to the value of an evaluated expression:
const myBool = 15 > 50; //15 is not greater than 50, so myBool will be assigned to false
console.log(myBool); //prints false
console.log(!myBool); //prints true
//we can apply the ! operator at the time of assignment as well:
const myNewBool = !(15 > 50); //the expression inside the parenthesis evaluates to false, and the ! operator toggles it to true
console.log(myNewBool); //prints true
console.log(!myNewBool); //prints false
Kind of, yes. The opposite of true is false. They are the only two values in the Boolean data type set. NOT is a boolean operator that negates, or toggles the value it is paired up with (operand). When paired with true, the result is false. When paired with false the result is true.
It doesn’t end there, though, just the ‘opposite’, it begins there. NOT can be paired up with any value or expression. This time it negates the boolean evaluation of the expression. To make this evaluation the truthiness is assessed.
value truthiness (truth value)
All of these values can be negated, that is toggled to true.
! 0 => true
Note that the toggled outcome is always a boolean.
The NOT operator takes only one operand and has higher precedence that many operators so is often the first expression to be evaluated. That is why above we use grouping to allow the expression in brackets to be evaluated first, then NOT is applied to the outcome.
In the instructions for this exercise I am still getting a Red X and in the Main.js screen I am getting " Did you log 'time to sleep' ? Not a big deal I think because I can move forward anyway… just wondering why I can’t get the green check mark? I am getting the correct log to the console. Below is the code I have…
mft… you are very quick to reply… thanks for that. Just so I understand: I guess the instructors want us to type exactly what they have written?? In playing around with the exercise, I had originally typed “Go to bed!” instead of “Time for Sleep”… and “Not tired yet” for “Not time for bed”. I wasn’t really watching for the green check mark and my console was printing properly… depending on how I changed the values in the variables or which Logical Operator I used. It was only later that I noticed the red X and that I couldn’t move forward. In the end, text that is logged to the console is at the discretion of the programmer… correct???