Start learning free

LESSON 94 OF 675 · CONDITIONAL STATEMENTS

Logical Operators: Not (!)

Lesson

The logical not operator (!) is used to invert the value of a boolean condition. For example:

7 <= 10 // Evaluates to true
!(7 <= 10) // Evaluates to false

12 != 12 // Evaluates to false
!(12 != 12) // Evaluates to true

!true // Evaluates to false

It is often used with boolean variables:

let hungry = true;
if(!hungry) {
    console.log("I can wait a little longer");
}
else {
    console.log("I need food!");
}

!hungry evaluates to false, so we enter the else and print "I need food!".

Assignment
Follow the Coding Tutorial and play with the not operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check