DEV Community

Cover image for JavaScript Equals: JavaScript ‘===’ vs ‘==’Comparison Operator
Tanvir Ahmed
Tanvir Ahmed

Posted on

JavaScript Equals: JavaScript ‘===’ vs ‘==’Comparison Operator

In JavaScript, the equality operators "==" and "===" are used to compare two values, but they work differently in terms of how they handle data types. Here’s a simple explanation:

1. == (Loose Equality):

The JavaScript equals or loose equality operator ( == ) checks if two variables or values are equal. It returns true if two values are of equal value, even if they're of different types. Conversely, it returns false if the values aren't of equal value.

Image description

Here, JavaScript converts the string '1' into a number 1 and then compares them, so it returns true.

  • What it does: It checks if two values are equal, but ignores the data type.
  • Type conversion (coercion): JavaScript automatically converts one or both values to the same type before comparing them.

Others Example:

i). 0 == false is true (because false is converted to 0)
ii). null == undefined is true (they are considered loosely equal)

Problem: This automatic type conversion can sometimes lead to unexpected results, so it’s generally considered less reliable.

2. === (Strict Equality):

The strict equality (===) operator checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Image description

Here, JavaScript does not convert the string '1' into a number. Since 1 is a number and '1' is a string, it returns false.

  • What it does: It checks if two values are exactly equal, including both the value and the data type.
  • No Type conversion: It does not perform type conversion; the types must match for the comparison to return true.

Others Example:

i). 0 == false is false (because 0 is a number and false is a boolean)
ii). null == undefined is false (they are of different types)

Summary:

  • == (loose equality) compares values after converting them to the same type.

  • === (strict equality) compares values without any type conversion.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay