-1

I do not mean deep comparison. I just want to know if two variables refer to the same instance. Should I use a==b or a===b? Can two variables point to the same memory but with different types? Because javascript has no such concept as class in C++, I do not know what an object's type is. Do all objects have the same type: "object" so === decides their types are equal? If so, === would be the same as ==.

William
  • 761
  • 2
  • 10
  • 27
  • 1
    `Can two variables point to the same memory but with different types?` No. A variable at any given time refers to a specific value with a specific type. – nicholaswmin Nov 29 '19 at 07:34
  • 2
    The duplicate is wrong. This is a different question. I'm sure there's a dupe somewhere but the one linked is not very relevant in the context of this question. – nicholaswmin Nov 29 '19 at 07:40
  • 1
    Though the question likely is a duplicate, the linked question is not an answer to this one, in my opinion. – briosheje Nov 29 '19 at 07:51
  • 1
    Just to add something to the boilerplate, I always reference this on such kind of questions: [Always use 3 equals unless you have a good reason to use 2](https://dorey.github.io/JavaScript-Equality-Table/?utm_content=buffer4f1b9). – briosheje Nov 29 '19 at 07:56

2 Answers2

1

From A Drip of Javascript: Object Equality in Javascript:

... Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference. That comparison by reference basically checks to see if the objects given refer to the same location in memory. Here is an example of how that works.

var jangoFett = {
    occupation: "Bounty Hunter",
    genetics: "superb"
};

var bobaFett = {
    occupation: "Bounty Hunter",
    genetics: "superb"
};

var callMeJango = jangoFett;

// Outputs: false
console.log(bobaFett === jangoFett);

// Outputs: true
console.log(callMeJango === jangoFett);
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
-2

you should use === because it will avoid error exceptions hard to be found. and about time consuming, === is better as well.

Oang Phạm
  • 127
  • 1
  • 7