Logical Operators Part 1
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 18 of 77.
Logical operators are used to check combinations of comparisons that return true or false.
For example, the following statement contains two comparisons:
Is 5 greater than 3 and less than 6?
| Operator | Meaning | Example |
|---|---|---|
&& | And - true if all operands are true | a && b |
|| | Or - true if any operand is true | a || b |
! | Not - true if the operand is false | !a |
Let's see some examples:
5 is greater than 3, and 1 equals 1:
let b1 = (5 > 3) && (1 == 1); // holds trueExplanation: All of the operands are true, so b1 will hold true (&& operation is true if both operands are true) .
5 is not equal to 4, or five equals to 2:
let b2 = !(5 == 4) || (5 == 2); // holds trueExplanation: The first operand (5 != 4) is true so b2 is also true (|| operation is true if either one of the operands is true)
1 is not equal to 1 or false:
let b3 = !(1 == 1) || false; // holds falseExplanation: All of the operands are false, so b3 will hold false (|| operation).
not (3 greater than 4):
let b4 = !(3 > 4); // holds trueExplanation: The operand is false, so b4 will hold true (! operation).
not (5 greater than 10 or 5 greater than 1):
let b5 = !(5 > 10 || 5 > 1); // holds falseExplanation: 5 > 10 || 5 > 1 is true (one of the operands is true), so in total b5 is false (! operation).
Challenge
BeginnerYou are given a code. Replace the question marks of the variables b1 and b2 so that b3 holds true.
There are many right solutions.
Cheat sheet
Logical operators are used to check combinations of comparisons that return true or false.
| Operator | Meaning | Example |
|---|---|---|
&& |
And - true if all operands are true |
a && b |
|| |
Or - true if any operand is true |
a || b |
! |
Not - true if the operand is false |
!a |
Examples:
let b1 = (5 > 3) && (1 == 1); // true - both operands are true
let b2 = !(5 == 4) || (5 == 2); // true - first operand is true
let b3 = !(1 == 1) || false; // false - all operands are false
let b4 = !(3 > 4); // true - operand is false, so ! makes it true
let b5 = !(5 > 10 || 5 > 1); // false - expression inside is true, ! makes it falseTry it yourself
// Type your code below
let b1 = ?
let b2 = ?
let b3 = b1 || b2
// Don't change the line below
console.log(`b3 = ${b3}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False