Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript, is it guaranteed that {} !== {}?

Tags:

javascript

I have read Which equals operator (== vs ===) should be used in JavaScript comparisons?, but it didn't help me because it doesn't say whether objects can be reused by the JS virtual machine.

In Javascript, is it guaranteed that two objects created successively (and of course not by assigning one to another) have a different reference?

Consider the following snippet:

let a = {};
let b = {}; // b can be created at any time after a is created
let areEqual = a === b; // comparison can be done at any time after b is created

Is it guaranteed that areEqual is false? That some processor optimization won't make b re-use a's reference?

like image 433
yannick1976 Avatar asked Oct 20 '25 02:10

yannick1976


1 Answers

Yes! Triple Equals is a combination of Value & Type. When you compare non-primitive variables in javascript what you are really comparing is their reference id. In other words, in your example a will never equal b, nor will it with any amount of processor optimization.

like image 74
joshrathke Avatar answered Oct 21 '25 14:10

joshrathke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!