Categories
JavaScript Answers

How to create a JavaScript variable reference or alias?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.