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 OverflowJavaScript comparison operators: Identity vs. Equality - Stack Overflow
Comparison Operator in JavaScript - Stack Overflow
How do the JavaScript relational comparison operators coerce types? - Stack Overflow
Basic JavaScript - Comparison with the Inequality Operator - Examples!
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
The difference is that ==, <=, >= and != will do type coercion — for example, force a string to be evaluated as a number. ===, <==, >==, and !== will not do type coercion. They will compare a string to a number, and since the string "1" is not the same as the numeric value 1, the result is false.
Reference is here:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators
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
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:
- Two values are the same only if they have the same value.
- 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.