Menu
Coddy logo textTech

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 comparison
let b = 0 == false; // Holds true, because the boolean false is converted to the number 0 before comparison

Strict 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 string
let b = 0 === false; // Holds false, because no type conversion is performed, and the number 0 is not equal to the boolean false

In 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 icon

Challenge

Beginner

You are given a code with three variables: a, b, and c.

Your task is to perform the following comparisons and print the results:

  1. Check if a is loosely equal to b and store the result in d.
  2. Check if a is strictly equal to c and store the result in e.
  3. Check if b is loosely not equal to c and store the result in f.
  4. Check if a is strictly not equal to b and store the result in g.

Use the given variables and comparison operators to complete the task.

Cheat sheet

JavaScript has two types of equality operators:

Loose equality (==) performs type coercion before comparison:

5 == "5"   // true - string "5" converted to number 5
0 == false // true - boolean false converted to number 0

Strict equality (===) compares without type conversion:

5 === "5"   // false - number vs string, no conversion
0 === false // false - number vs boolean, no conversion

Similarly for inequality operators:

  • Loose inequality (!=) - performs type coercion
  • Strict inequality (!==) - no type conversion

It's recommended to use strict equality (===) to avoid unexpected results from type coercion.

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}`)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals