How can I pare 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 parison 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)
How can I pare 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 parison 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)
Share
Improve this question
edited Mar 9, 2018 at 11:37
Red
asked Mar 9, 2018 at 10:13
RedRed
7,36410 gold badges53 silver badges95 bronze badges
10
-
You will have to check the contents of the objects. You can try
a.every(x=> b.includes(x))
. – Rajesh Commented Mar 9, 2018 at 10:19 - 1 Possible Duplicate: stackoverflow./questions/1068834/… – Rajesh Commented Mar 9, 2018 at 10:24
- @Rajesh Duplicate is about object parison, my question is about paring reference types. – Red Commented Mar 9, 2018 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 Commented Mar 9, 2018 at 10:29
- I dont think there are reference types that are not objects nor objects that are not reference types. – ASDFGerte Commented Mar 9, 2018 at 10:30
4 Answers
Reset to default 2To pare 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));
})();
a
and b
are not exactly the same. If the type is a reference type, paring 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 paring 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) -----------------------|
[]
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 bees 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
bees true.
If both operands are objects, then JavaScript pares 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 pared by their value, while objects like arrays, dates, and plain objects are pared by their reference. That parison by reference basically checks to see if the objects given refer to the same location in memory.
Please see below snippet for parison 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);
})();