Getting Started
Level 1
- 4 - Comments
- 5 - Displaying Text
- 6 - Variables - Let
- 7 - Variables - Constants
- 8 - Arithmetic Operators
- 9 - Functions
- 10 - If/Else Statements
- 11 - Comparison Operators
- 12 - If/Else Loops
- 13 - Boolean Operators
- 14 - Level 1 Complete!
Level 2
Level 3
Useful Links
Step 13 - Boolean Operators
Putting an exclamation mark !
before a Boolean variable gives
the opposite value. The !
is called a “not” operator when
used this way.
The result of a comparison is a Boolean value, we can save it to a variable
const bool = (1 < 2);
Then we can check if bool
is true or false by using console.log
console.log(bool);
- this will return true
console.log(!bool);
- “not true”, therefore returns false
console.log(bool);
- The original value isn’t affected, still returns true
We can also assign a Boolean value straight to a variable with the keywords
true
and false
:
const aLie = false;
let previousStatement = true;
Challenge:
Try inverting a true or false value and print the result to the console
Answer
const human = true;
console.log(!human) // should return false