Red
Red

Reputation: 7299

How to compare two types JavaScript?

How can I compare two the same types against each other in JavaScript?

So a == b should return true, because they are both arrays.

So it doens't matter what the contents of the variable are. The comparison should be something like this;

var a = [];
var b = [];

var c = "this is a string";
var d = "this is also a string";

if(a type == b type) // true because both arrays
if(c type == d type) // true because both strings
if(a type == d type) // false because not the same type (string) or (array)

Upvotes: 1

Views: 6314

Answers (4)

Anto Livish A
Anto Livish A

Reputation: 332

To compare two types in javascript, use typeof.

(function () {
  var a = [];
  var b = [];
  var c = a;

  var d = "a";
  var e = "a";

  console.log(typeof(a) == typeof(b));
  console.log(typeof(a) == typeof(c));
  console.log(typeof(d) == typeof(e));
})();

Upvotes: 2

Atika Vaniya
Atika Vaniya

Reputation: 1

If both operands are objects, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.

The variables a and b refer to two objects with identical properties, but they are each distinct instances. On the other hand a and c both refer to the same instance.

The reason for this is that internally JavaScript actually has two different approaches for testing equality. 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.

Please see below snippet for comparison of objects in Javascript.

(function () {
  var a = [];
  var b = [];
  var c = a;
  
  var d = "a";
  var e = "a";

  console.log(a == b);
  console.log(a == c);
  console.log(d == e);
})();

Upvotes: 0

Saikat Hajra
Saikat Hajra

Reputation: 680

[] is Javascript Array type Object.

== does type check first.

when you are doing a == b although they are both array but they are different array object like they have different values and references.So it becomes false

when you are assigning c = a. It's called passing reference of a array to c.

therefore they are pointing to same array object. so a == c becomes true.

Upvotes: 1

Suren Srapyan
Suren Srapyan

Reputation: 68645

a and b are not exactly the same. If the type is a reference type, comparing them just checks their references. In this case a and b are arrays, but they have separate objects in the memory and separate references to them.

When you assign a to c, it just copies the reference from a and assigns it to c. And now c also refers to the same object as a. So while comparing a and c returns true, because they have the same reference value(address of the memory).

Something like this. (number in the [] is the address of the memory, number in the () is the value)

 a(0x1616) ---------> [0x1616] <--|
 b(0x1717) ---------> [0x1717]    |
 c = a;                           |
 c(0x1616) -----------------------|

Upvotes: 1

Related Questions