Skip to content
Shameem's Note
Shameem Reza JavaScript, WordPress & SwiftUI
Shameem's Note

JavaScript, WordPress & SwiftUI

BUY ME A COFFEE
  • Home
  • About
  • Now
Shameem's Note

JavaScript, WordPress & SwiftUI

January 20, 2023January 26, 2023

Strict vs Loose Equality in JavaScript: Understanding the Differences and When to Use

JavaScript has two ways to check for equality between values: strict equality (===) and loose equality (==). It is important to understand the difference between the two because they can give different results in certain situations.

In this article, we will go over the differences and when to use each type of comparison.

Strict Equality (===)

Strict equality, also known as “triple equals”, compares both the value and the type of two operands. It returns true if the operands are equal and of the same type.

JS Comparison Table
JS Equality Comparison Table by Dorey

Strict equality example:

1 === 1 // true
1 === "1" // false

In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, so the comparison returns false.

Loose Equality (==)

Loose equality, also known as “double equals”, compares the values of two operands without considering their types. It returns true if the operands are equal, regardless of their type.

JS Comparison Table by dorey
JS Equality Comparison Table by dorey

Loose equality example:

1 == 1 // true
1 == "1" // true

In the first example, both operands are of the same type (number) and have the same value, so the comparison returns true. In the second example, one operand is a number and the other is a string, but their values are the same, so the comparison returns true.

When to Use Each Type of Comparison

In general, it is best to use strict equality whenever possible because it avoids the confusion that type coercion can cause. However, there are some cases where loose equality is useful:

  • When comparing a variable to null or undefined
  • When comparing a variable to a boolean
  • When comparing a variable that may be a string or a number
// Use strict equality when comparing variables of the same type
let age = 25;
let legalAge = 21;
console.log(age === legalAge); // false

// Use loose equality when comparing variables that may be a string or a number
let input = "25";
let number = 25;
console.log(input == number); // true

// Use loose equality when comparing a variable to null or undefined
let name;
console.log(name == undefined); // true

In conclusion, understanding the difference between strict and loose equality in JavaScript is important for writing accurate and efficient code. Strict equality (===) is a more reliable way to check for equality in JavaScript, but loose equality (==) can be useful in certain situations. As a best practice, always use strict equality(===) unless you have a specific reason to use loose equality(==).

More resources:

  • MDN web docs: Equality comparisons and sameness
  • JavaScript Equality Table by w3schools.com
  • JavaScript Equality Comparison table by Dorey
JavaScript

Post navigation

Previous post
Next post

Woth to read

JavaScript Mastering the Concept of Truthy and Falsy Values in JavaScript

Mastering the Concept of Truthy and Falsy Values in JavaScript

January 23, 2023January 26, 2023

JavaScript is a widely used programming language for creating interactive websites and web applications. One…

Read More
JavaScript Ionic vs React Native 2020

Ionic vs React Native: What’s best for You?

April 1, 2020May 22, 2020

Ionic vs React Native: what is the best framework? Yeah! 2020 has begun and here…

Read More
JavaScript TypeScript vs JavaScript

TypeScript vs JavaScript: What are the differences?

November 10, 2022

If you’ve ever worked on a website development project, you’ve undoubtedly come across JavaScript. Indeed,…

Read More
Shameem Reza

I am the Father of a son, blessed with a pious wife. I am a self-taught iOS developer, SwiftUI Addicted and WordPress Ninja.

Over 80,000+ Readers

Get fresh content from Shameem.

  • Twitter
  • GitHub
  • LinkedIn
SwiftUI
JavaScript
Wordpress
Tips & Tricks

Recommended Resources

WordHero: 50+ AI writing tool to write sales and marketing emails that sell.

Rytr: AI Writing Tool to generate high-quality content.

Wave.video: Live Streaming Studio, Video Editor, Thumbnail Maker, Video Hosting, Video Recording, and Stock Library combined in one platform.

Ocoya: An all-in-one marketing solution that helps you effortlessly create, automate, and manage high-converting campaigns.

Join 3500+ Developers Getting Early Access To My Posts!

I learned these in the past 10 years by building a digital product development agency, and dozens of different web & mobile applications for clients and myself. Sign up to get updates when I write something new. No spam ever.

© 2023 Shameem's Note | Contact - Privacy - Disclosure