Strict vs Loose Equality
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 16 of 77.
In JavaScript, there are two types of equality operators: loose equality (==) and strict equality (===).
Loose equality (==) checks if two values are equal after performing type coercion. This means that if the operands have different types, JavaScript will try to convert them to a common type before making the comparison.
For example:
let b = 5 == "5"; // Holds true, because the string "5" is converted to the number 5 before comparisonlet b = 0 == false; // Holds true, because the boolean false is converted to the number 0 before comparisonlet c = "" == false; // Holds true, because the empty string is converted to the number 0 before comparisonStrict equality (===), on the other hand, checks if two values are equal without performing any type conversion. If the operands have different types, they are considered not equal.
For example:
let b = 5 === "5"; // Holds false, because no type conversion is performed, and a number is not equal to a stringlet b = 0 === false; // Holds false, because no type conversion is performed, and the number 0 is not equal to the boolean falseIn general, it's recommended to use strict equality (===) in most cases to avoid unexpected results due to type coercion.
Similarly, there are also loose inequality (!=) and strict inequality (!==) operators that work in a similar way, but check for inequality instead of equality.
Loose inequality (!=) checks if two values are not equal after performing type coercion, while strict inequality (!==) checks if two values are not equal without performing type conversion.
Challenge
BeginnerYou are given a code with three variables: a, b, and c.
Your task is to perform the following comparisons and print the results:
- Check if
ais loosely equal toband store the result ind. - Check if
ais strictly equal tocand store the result ine. - Check if
bis loosely not equal tocand store the result inf. - Check if
ais strictly not equal toband store the result ing.
Use the given variables and comparison operators to complete the task.
Try it yourself
// Given variables
let a = 10
let b = "10"
let c = true
// Type your code below
// Don't change the line below
console.log(`d = ${d}`)
console.log(`e = ${e}`)
console.log(`f = ${f}`)
console.log(`g = ${g}`)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