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);

8
  • 2
    Since when does strict equality === have to do with memory location? Commented Dec 7, 2018 at 17:24
  • 1
    The == operator performs type coercion, so no, they don't necessary point to the same memory. Commented Dec 7, 2018 at 17:24
  • See this answer stackoverflow.com/questions/359494/… Commented Dec 7, 2018 at 17:26
  • 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.". It has nothing to do with memory. Commented Dec 7, 2018 at 17:49
  • 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)" Commented Dec 7, 2018 at 18:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.