-1
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

why this is returning true

If the code was

const abc = 5
const bcd = abc

console.log(abc === bcd) ///true here make sense

I seem to be be really confused with the first case since abc and bcd are two different variables and have no relation between them

Any related article or blog would really help.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
Shivam Gupta
  • 159
  • 6
  • 4
    This is just how it was specified to behave? Primitives are compared by value. – ASDFGerte Nov 10 '21 at 18:41
  • 4
    === don't compare memory location, but value and type. So, not surprising – Vítor França Nov 10 '21 at 18:43
  • 4
    So how would you ever compare that two strings, numbers, etc would equal each other if they have to be in the same memory location? – epascarello Nov 10 '21 at 18:43
  • 1
    https://262.ecma-international.org/5.1/#sec-11.9.6 – epascarello Nov 10 '21 at 18:45
  • 1
    Maybe rather use the contemporary spec: https://tc39.es/ecma262/#sec-isstrictlyequal – ASDFGerte Nov 10 '21 at 18:45
  • 1
    Does this answer your question? [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – eglease Nov 10 '21 at 18:55
  • @epascarello "*So how would you ever compare that two strings, numbers*" Java strings you need to compare with `str.equals("hello")` otherwise the identity comparison with `==` *might* fail (depends on whether or not you're comparing *the same object* but that's not always clear, since strings might be interned). However, that means that you get an NPE if `str = null` which in turn makes the following comparison safer: `"hello.equals(str)` but also a bit weirder. And doesn't help if you have two variables. So, in short the answer to your question is: super inconveniently. – VLAZ Nov 10 '21 at 19:03

2 Answers2

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
Turtlefight
  • 9,420
  • 2
  • 23
  • 40
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.

Rakesh Poddar
  • 183
  • 3
  • 11
  • "*Objects are not compared by value:*" they **are**. However, the value of an object is its reference. That's why comparing objects compares the references, not because there is a completely separate comparison operation defines only for objects. – VLAZ Nov 10 '21 at 18:58
  • Actually, I wrote that line from a Book. I believe Objects are compared by their reference . Just after you wrote, I thought of searching and found everywhere this => `Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference.` – Rakesh Poddar Nov 10 '21 at 19:05
  • Apparently "everywhere" is wrong then. [It's always pass by value, but for objects the value of the variable is a reference.](https://stackoverflow.com/a/5314911) - found this in "everywhere" and it claims otherwise. As do the specs: "*If x and y are the same Object value, return true. Otherwise, return false.*" for how object equality works in the [`SameValueNonNumeric`](https://262.ecma-international.org/12.0/#sec-samevaluenonnumeric) which is the algorithm referenced by all the `SameValue*` algorithms when they eventually have to compare objects. I stand by what I said. – VLAZ Nov 10 '21 at 19:16