1

In below code snippet a==b return true, i.e they point to same memory location, hence they will have same value. I would like to know, how JS engine knows a===b is false. How is type information determined when 2 different types point to same memory location?

Edit 1: From comments it looks like my question might not be clear. I totally understand difference between == and === in terms of usage in JS language. I am more interested in knowing how JS engine saves type information for null and undefined. As per my understanding variables a & b point to same memory location that is why I get a==b, if this understanding is wrong, please correct me.

Edit 2: Ok I will put my question in another way. How typeof operator knows a is object and b is undefined despite having a==b.

var a = null;
var b = undefined;

console.log(a==b);

console.log(a===b);

console.log(typeof a);
console.log(typeof b);
John
  • 371
  • 3
  • 11
  • 2
    Since when does strict equality `===` have to do with memory location? – j08691 Dec 07 '18 at 17:24
  • 1
    The `==` operator performs type coercion, so no, they don't necessary point to the same memory. – Greg Burghardt Dec 07 '18 at 17:24
  • See this answer https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – Ambrish Pathak Dec 07 '18 at 17:26
  • "_As per my understanding variables a & b point to same memory location that is why I get a==b_" No, they don't. Take a look at [the language specification](http://www.ecma-international.org/ecma-262/9.0/index.html#sec-abstract-equality-comparison). For `==`: "_If x is null and y is undefined, return true._". For `===`: "_If Type(x) is different from Type(y), return false._". – Ivar Dec 07 '18 at 17:36
  • typeof null is object, but typeof undefined is "undefined" – ABOS Dec 07 '18 at 17:36
  • 1
    @Mohit The fact that `==` returns true does **NOT** mean that they are on the same memory. The reason `==` returns true is because the specification says so. "[_If x is null and y is undefined, return true._](http://www.ecma-international.org/ecma-262/9.0/index.html#sec-abstract-equality-comparison)". It has nothing to do with memory. – Ivar Dec 07 '18 at 17:49
  • @Ivar ok, so specification says that null == undefined should return true. Can you also confirm that typeof figures out type of a variable based on size of memory allocation? As per my understanding 1 and "1" type at runtime is determined just by the difference of memory allocation size for string and int in JS – John Dec 07 '18 at 18:04
  • 1
    "_As per my understanding 1 and "1" type at runtime is determined just by the difference of memory allocation size for string and int in JS_" @Mohit I'm not sure what gives you that impression. How the engine implements it is up to that specific Engine. (Not every browser uses the same.) I'm fairly confident that internally the type is just stored along side the value of the variable. For `1` and `"1"` it is again the specification that says they should be true for `==`: "_If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y)_" – Ivar Dec 07 '18 at 18:14

2 Answers2

0

In the code snippet added in the question:

var a = null; var b = undefined; console.log(a==b); console.log(a===b);

console.log(a==b) returns true because == uses type coercion to check the equality of both the variables. Therefore, null and undefined are thought of as equals.

console.log(a===b) returns false because === does not use type coercion. For ===, null and undefined are not same types and it doesn't care about checking deep equality when the operands aren't of the same type.

This has got nothing to do with memory locations.

Saad Patel
  • 49
  • 7
  • JS is dynamically typed language, hence when values are loaded from memory it can determine its type base on memory allocation. That is how JS engine knows 1 is number and "1" is string with typeof operator. My question is if null and undefined are both have same value how is type information determined at runtime. – John Dec 07 '18 at 17:40
  • @Mohit null and undefined are two different things. `undefined` is a type in itself. it is when a variable has not been assigned any value. `null` on the other hand is an assignment value and an object. They DO NOT have the same value at all. – Saad Patel Dec 07 '18 at 17:57
0
a = null, b= undefined;
a == b /* only check their values */
a === b /* also check their types + values */
typeof a == typeof b // false

typeof "variable" gives the type of the variable.

Vinayk93
  • 353
  • 1
  • 6