1

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)
Red
  • 6,599
  • 9
  • 43
  • 85
  • You will have to check the contents of the objects. You can try `a.every(x=> b.includes(x))`. – Rajesh Mar 09 '18 at 10:19
  • 1
    Possible Duplicate: https://stackoverflow.com/questions/1068834/object-comparison-in-javascript – Rajesh Mar 09 '18 at 10:24
  • @Rajesh Duplicate is about object comparison, my question is about comparing reference types. – Red Mar 09 '18 at 10:24
  • First, I did not mark duplicate because I was not sure. Second, can you explain what do you mean by *reference types*? – Rajesh Mar 09 '18 at 10:29
  • I dont think there are reference types that are not objects nor objects that are not reference types. – ASDFGerte Mar 09 '18 at 10:30
  • like `object == object`, `number == number`, `string == string`. Whats the correct word for that? I guess its not reference types. If someone can let me know, I will edit the question. – Red Mar 09 '18 at 10:33
  • numbers and strings are primitives, they behave differently. – ASDFGerte Mar 09 '18 at 10:34
  • Sorry I was a bit unclear. See updated question and anwser of @AntoLevishA – Red Mar 09 '18 at 10:47
  • I'm on a editing spree, it finaly starts to be clear what I really want. Sorry guys – Red Mar 09 '18 at 11:31
  • What you want is strictly speaking impossible, though it can be done in most cases, see [duck typing](https://en.wikipedia.org/wiki/Duck_typing). Javascript is not a typed language (apart from primitives + object). You can get static compile-time typechecks with e.g. [typescript](https://www.typescriptlang.org/) (or flow afaik), but at runtime, there is no type metadata. – ASDFGerte Mar 09 '18 at 11:39

4 Answers4

2

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));
})();
Anto Livish A
  • 322
  • 4
  • 15
  • Ha, this is what I meant – Red Mar 09 '18 at 10:44
  • Also note that the parentheses are superfluous. `typeof` isn’t a function, so `typeof a` is enough. If you want to differentiate between plain objects and arrays, you need to compare their _prototypes_ or _prototype chains_, not their types. – Sebastian Simon Mar 09 '18 at 11:05
  • So this is not recommendend @Xufox @ASDFGerte? – Red Mar 09 '18 at 12:28
  • @Red It really depends on what you intend to do. Usually, when using variables, you should know in advance which type they have. If, for example, you specifically need to know whether something is an array, you could use `Array.isArray(a)`. There’s no real test for “plain objects”, though. – Sebastian Simon Mar 09 '18 at 15:40
1

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) -----------------------|
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

[] 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.

Saikat Hajra
  • 670
  • 3
  • 12
0

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