Logical operator !

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Logical operator !

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does !true give?
A true
B false
2. What does !false give?
A false
B true
3. How do you read the ! operator in plain English?
A not
B and
4. Which line is true when score is 40: !(score >= 50) or (score >= 50)?
A !(score >= 50)
B (score >= 50)
5. Which code correctly reverses the whole comparison?
A !(age > 12)
B !age > 12

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1!true Alet failed = !(mark >= 50);
2!(score >= 50) BThe ! is placed before score only, not before the whole bracketed comparison. It flips score first, not the result of score >= 50.
3Put ! before brackets to reverse the result of a whole comparison Clet isOpen = !isClosed;
4Reading ! as 'not': let isOpen be the opposite of isClosed DGives false, because ! reverses the boolean value
5if (!score >= 50) { E!false reverses false into true, not false. The programmer forgot that ! flips the value.
6let isNight = !false; // want false FTrue when score is NOT 50 or more, so score is below 50

Definitions

!

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
console.log(_true);
2.
let isDone = false; console.log(_isDone);
3.
let score = 40; let failed = _(score >= 50); console.log(failed);
4.
let isOpen = true; let isClosed = !______; console.log(isClosed);
5.
let raining = false; if (!_______) console.log("Go outside");

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let score = 42;
B let failed = !score >= 50;
C console.log(failed);
Question 2
A let isOpen = true;
B let isClosed = ~isOpen;
C console.log(isClosed);
Question 3
A let age = 15;
B let isAdult = age >= 18;
C if (!isAdult {
D console.log("Minor");
E }
Question 4
A let score = 30;
B let target = 50;
C let passed = score >= target;
D let failed = !!passed;
E console.log(failed);
Question 5
A let hasTicket = true;
B let late = false;
C if (hasTicket && late!) {
D console.log("Enter");
E }

Answer Key

Logical operator !
Section A: Quick Questions
Q1
B — false
Q2
B — true
Q3
A — not
Q4
A — !(score >= 50)
Q5
A — !(age > 12)
Section B: Match the Pairs
Q1
!true → Gives false, because ! reverses the boolean value
Q2
!(score >= 50) → True when score is NOT 50 or more, so score is below 50
Q3
Put ! before brackets to reverse the result of a whole comparison → let failed = !(mark >= 50);
Q4
Reading ! as 'not': let isOpen be the opposite of isClosed → let isOpen = !isClosed;
Q5
if (!score >= 50) { → The ! is placed before score only, not before the whole bracketed comparison. It flips score first, not the result of score >= 50.
Q6
let isNight = !false; // want false → !false reverses false into true, not false. The programmer forgot that ! flips the value.
Section C: Definitions
Q1
! — The not operator. It reverses a boolean: !true is false and !false is true. Put it before a value or bracket, like !(score >= 50). Read it as 'not'.
Section D: Fill in the Blanks
Q1
console.log(!true);
Q2
let isDone = false; console.log(!isDone);
Q3
let score = 40; let failed = !(score >= 50); console.log(failed);
Q4
let isOpen = true; let isClosed = !isOpen; console.log(isClosed);
Q5
let raining = false; if (!raining) console.log("Go outside");
Section E: Spot the Error
Q1
B — Without brackets, ! only reverses score, not the whole comparison. Put the comparison in brackets: !(score >= 50).
Q2
B — The symbol for 'not' in JavaScript is !, not ~. It should be !isOpen so true becomes false.
Q3
C — The closing bracket of the if condition is missing. It should be if (!isAdult) {.
Q4
D — Two ! symbols reverse the boolean twice, so failed would just equal passed. Use one !, read as 'not': !passed.
Q5
C — The ! operator goes before the value, not after it. It should be !late, read as 'not late'.