Sometimes, we want to create a JavaScript variable reference or alias.
In this article, we’ll look at how to create a JavaScript variable reference or alias.
How to create a JavaScript variable reference or alias?
To create a JavaScript variable reference or alias, we can create an object.
For instance, we write
const obj = { x: 1 };
const aliasToObj = obj;
aliasToObj.x++;
alert(obj.x);
to create the obj
object and assign obj
to aliasToObj
.
aliasToObj
references obj
, so when we update its value of x
with aliasToObj.x++
.
obj.x
also changes value.
Therefore, aliasToObj.x
and obj.x
are both 2.
Conclusion
To create a JavaScript variable reference or alias, we can create an object.