Logical Operators Part 3
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 21 of 77.
When working with logical expressions, sometimes we need to simplify or rearrange them.
! (not) in front of two conditions joined by && (and), you can split it into two separate parts. The && (and) becomes || (or), and each part gets its own ! (not):
!(A && B) is the same as (!A) || (!B)
For example:
// Let's check if a number is NOT (between 1 and 10)
let number = 15
// These two expressions are equivalent:
let result1 = !(number >= 1 && number <= 10)
let result2 = !(number >= 1) || !(number <= 10)
console.log(result1) // True
console.log(result2) // TrueThe opposite is also correct: !(A || B) is the same as (!A) && (!B)
For example:
// Checking if a person is NOT (a student or employed)
let is_student = false
let is_employed = false
// These two expressions are equivalent:
let result1 = !(is_student || is_employed)
let result2 = !is_student && !is_employed
console.log(result1) // True
console.log(result2) // TrueChallenge
BeginnerYou're helping a pet shop create a system to determine if they can sell a pet to a customer.
initialize the following variables:
has_licensewith the valuetruehas_spacewith the valuefalsehas_experiencewith the valuetrue
Write the following logical expressions to determine if:
can_sell_regular_pet: Customer needs EITHER a license OR experience, AND must have spacecan_sell_exotic_pet: Customer needs BOTH a license AND experience, AND must have spacecannot_sell_any_pet: Customer has NO license AND NO experience, OR has NO space
Cheat sheet
De Morgan's Laws allow you to simplify logical expressions by moving the ! (not) operator:
!(A && B) is the same as (!A) || (!B)
// NOT (between 1 and 10)
let result1 = !(number >= 1 && number <= 10)
let result2 = !(number >= 1) || !(number <= 10) // equivalent!(A || B) is the same as (!A) && (!B)
// NOT (student or employed)
let result1 = !(is_student || is_employed)
let result2 = !is_student && !is_employed // equivalentTry it yourself
// Initialize variables
// Calculate conditions
let can_sell_regular_pet =
let can_sell_exotic_pet =
let cannot_sell_any_pet =
// Don't delete the lines below
console.log("Can sell regular pet:", can_sell_regular_pet)
console.log("Can sell exotic pet:", can_sell_exotic_pet)
console.log("Cannot sell any pet:", cannot_sell_any_pet)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