Is there a way to know that 2 javascript variable point to the same memory address ?
var my_var = { id: 1, attribute: "myAttribute" } var copy = my_var; //someting like if(copy === my_var) return true;
For reference type like objects, == or === operators check its reference only. here a==b will be false as reference of both variables are different though their content are same.
“Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.
A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items.
Use the reserved keyword var to declare a variable in JavaScript. Syntax: var <variable-name>; var <variable-name> = <value>; A variable must have a unique name.
You can't alias variables like you can in C. In javascript, something like
var x = 1; var y = x y = 4; // x is still 1
will always be the case.
However, objects are always passed by reference
var x = { one: 1, two: 2 }; var y = x; y.one = 100; // x.one is now 100
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With