The equality operator will attempt to make the data types the same before making the comparison. On the other hand, the identity operator requires both data types to be the same as a prerequisite.

There are quite a few other posts out there similar to this questions. See:

How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? (has a nice comparison chart)
Which equals operator (== vs ===) should be used in JavaScript comparisons?

In practice, the identity operator comes in really handy when you want to be certain that a boolean value is true or false since...

Copy1 == true     => true
true == true  => true
1 === true    => false
true === true => true
Answer from Michael Copeland on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Expressions_and_operators
Expressions and operators - JavaScript | MDN
A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values.
Discussions

JavaScript comparison operators: Identity vs. Equality - Stack Overflow
I've been trying to understand the difference between JavaScript's comparison operators: identity and equality. From what I've read, if you check the equality of two objects using ==, JavaScript wi... More on stackoverflow.com
🌐 stackoverflow.com
Comparison Operator in JavaScript - Stack Overflow
const abc = 5; const bcd = 5 console.log(abc===bcd)/// return true This result is surprising to me. Since abc will be assigned a different memory location ...and bcd has also a different location... More on stackoverflow.com
🌐 stackoverflow.com
How do the JavaScript relational comparison operators coerce types? - Stack Overflow
What rules apply for the JavaScript relational comparison operators when the operands are of different types? For example, how is true > null evaluated? I can type this into my developer consol... More on stackoverflow.com
🌐 stackoverflow.com
Basic JavaScript - Comparison with the Inequality Operator - Examples!
Tell us what’s happening: Can someone Explain me the last two examples of this challenge? I’ve completed this challenge! But confused with the example in the instructions. How does 1 != true return false. If 1 is not equal to true, it should return true right??? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
November 27, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-comparison-operators
JavaScript Comparison Operators - GeeksforGeeks
The Strict equality Operator is used to compare the equality of two operands with type.
Published   January 28, 2026
🌐
web.dev
web.dev › learn › javascript › comparison
Comparison operators | web.dev
Comparison operators compare the values of two operands and evaluate whether the statement they form is true or false. The following example uses the strict equality operator (===) to compare two operands: the expression 2 + 2 and the value 4.
🌐
CodeSignal
codesignal.com › learn › courses › getting-started-with-javascript › lessons › diving-into-javascript-comparison-operators-a-beginners-guide
Diving Into JavaScript Comparison Operators
We used the logical AND operator && to chain the comparisons. The statement inside the parentheses returns true only if both conditions are true, i.e., both age >= 13 and age <= 18 are satisfied. ... In JavaScript, we also have the logical OR operator || used to chain multiple conditions.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Equality
Equality (==) - JavaScript | MDN
The equality (==) operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript comparison operators
JavaScript Comparison Operators
September 1, 2008 - The strict equality (===) and strict inequality (!==) operators perform strict comparison. These operators don't perform type conversion before performing comparison operation. There are some falsy values in JavaScript.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Comparisons
... Greater/less than: a > b, a < b. Greater/less than or equals: a >= b, a <= b. Equals: a == b, please note the double equality sign == means the equality test, while a single one a = b means an assignment.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Equality_comparisons_and_sameness
Equality comparisons and sameness - JavaScript | MDN
Strict equality is almost always the correct comparison operation to use. For all values except numbers, it uses the obvious semantics: a value is only equal to itself. For numbers it uses slightly different semantics to gloss over two different edge cases. The first is that floating point zero is either positively or negatively signed.
Top answer
1 of 2
1

In javascript all primitives (string, nubmer, bigint, boolean, undefined, symbol, null) are immutable and will be compared by value:

All primitives are immutable, i.e., they cannot be altered. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.


In comparison to that objects (this also includes arrays and functions, basically everything that is not a primitive) are mutable and will be compared by identity.

Example:

console.log("Primitives compare by value:");
console.log(5 === 5); // true
console.log("foo" === "foo"); // true
console.log(true === true); // true


console.log("Objects compare by identity:");
console.log({} === {}); // false
console.log([] === []); // false
console.log(function(){} === function(){}); // false


Primitive Wrappers

Javascript also has wrapper objects for the primitive types, which might be the source of your question.
These wrappers wrap a primitive value, and are - as the name suggests - objects. So for primitive wrapper instances your code would be correct:

let a = new Number(1);
let b = new Number(1);

console.log("Primitive wrappers are objects:");
console.log(a === a); // true
console.log(a === b); // false

2 of 2
1

There is a fundamental difference in JavaScript between primitive values (undefined,null, booleans, numbers, and strings) and objects (including arrays and functions)

I. Primitives are compared by value:

  1. Two values are the same only if they have the same value.
  2. If two distinct string values are compared, JavaScript treats them as equal if, and only if, they have the same length and if the character at each index is the same.

II. Objects are different than primitives.

Objects are not compared by value: Two distinct objects are not equal even if they have the same properties and values.

Therefore, Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types.

🌐
Scaler
scaler.com › topics › javascript › comparison-operators-in-javascript
Javascript Comparison Operators - Scaler Topics
February 14, 2024 - This article delves into the nuances of these operators, exploring how they compare values, manage type coercion, and ultimately shape the logic of JavaScript applications. Comparison operator in javascript are used to compare two operands (simply values). A boolean value (either true or false) ...
Top answer
1 of 1
26

JavaScript relational comparison operator type coercion is defined in the JavaScript specification, specifically in sections 11.8 to 11.8.5 which describe the operators, and sections 9.1 (ToPrimitive) and 9.3 (ToNumber) which describe the process of coercing the operands.

In short, the 4 comparison operators (<, >, <=, and >=) do their best to convert each operand to a number, then compare the numbers. The exception is when both operands are strings, in which case they are compared alphabetically.

Specifically,

  1. If an argument o is an object instead of a primitive, try to convert it to a primitive value by calling o.valueOf() or - if o.valueOf wasn't defined or didn't return a primitive type when called - by calling o.toString()

  2. If both arguments are Strings, compare them according to their lexicographical ordering. For example, this means "a" < "b" and "a" < "aa" both return true.

  3. Otherwise, convert each primitive to a number, which means:

  • undefined -> NaN
  • Null -> +0
  • Boolean primitive type -> 1 if true, +0 if false
  • String -> try to parse a number from the string
  1. Then compare each item as you'd expect for the operator, with the caveat that any comparison involving NaN evaluates to false.

So, this means the following:

console.log(true > null);           //prints true
console.log(true > false);          //prints true
console.log("1000.0" > 999);        //prints true
console.log("  1000\t\n" < 1001);   //prints true

var oVal1 = { valueOf: function() { return 1; } };
var oVal0 = { toString: function() { return "0"; } };

console.log(oVal1 > null);         //prints true
console.log(oVal0 < true);         //prints true
console.log(oVal0 < oVal1);        //prints true
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-comparison-and-logical-operators-in-javascript
Understanding Comparison and Logical Operators in JavaScript | DigitalOcean
August 25, 2021 - These are commonly used with conditional statements, and the if, else, and else if keywords, as well as the ternary operator. If you are interested in learning more about conditional statements first, refer to How To Write Conditional Statements in JavaScript. In JavaScript, there are a number of comparison operators that you can use to evaluate whether given values are different or equal, as well as if a value is greater than or less than another.
🌐
Simple Dev
simpledev.io › lesson › comparison-operators-js
Comparison operators - JavaScript
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Codecademy
codecademy.com › article › fwd-js-comparison-logical
Comparison and Logical Operators | Codecademy
Comparison operators allow us to assert the equality of a statement with JavaScript.
🌐
Mimo
mimo.org › glossary › javascript › equality-operator
Master Comparisons with JavaScript Equality Operator
JavaScript features a loose equality operator (==) and a strict equality operator (===). The choice between double equals and triple equals as a comparison operator depends on your needs for type coercion.
🌐
YouTube
youtube.com › watch
JavaScript Comparison and Logical Operators (With Examples) | JavaScript Tutorial - YouTube
JavaScript Comparison and Logical Operators are fundamental tools for writing conditional statements and controlling the flow of your code. In this JavaScrip...
Published   June 30, 2023