I’m familiar with pointers in C++ and find them very useful. Does JavaScript support anything similar to javascript pointers?
For example, I want to use a variable that points to another variable’s value or element like this:
var a = 1;
var b = "a";
document.getElementById(/* value pointed by b */).innerHTML = "Pointers";
Is there a way to achieve this pointer-like behavior in JavaScript, especially for more complex cases?
You’re right to draw that comparison, but JavaScript doesn’t support pointers the way C++ does. In JS, variables either store primitive values directly (like numbers or strings) or references to objects (like arrays or functions).
However, you can’t access or manipulate memory addresses like you can with pointers in C++. So, while object references might feel similar, you can’t create a variable that literally “points” to another variable’s memory location.
It’s a key difference that helps make JavaScript safer and more abstracted from lower-level memory management.
Hope this helps 
While JavaScript doesn’t have traditional pointers like C++, it does have something pretty close—object references.
Take this for example:
const obj = { val: 1 };
const ref = obj; // ref references the same object
ref.val = 42;
console.log(obj.val); // 42
Here, both obj
and ref
point to the same object in memory. So when you change a property using ref
, you’re actually updating obj
too.
It’s not pointer-level control, but it’s the closest JavaScript gets—references let you work with the same underlying data without duplicating it.
Hey @Shreshthaseth if you want to get a variable by its name as a string (like your b = “a” example), you could use the global object or a lookup object:
var a = 1;
var b = "a";
window[b]; //
gives the value of a if a
is global
Or better, store variables in an object:
const vars = { a: 1 };
const b = "a";
console.log(vars[b]); // 1
This lets you “indirectly” access values by name, which is somewhat similar to pointer dereferencing, but it’s not actual pointers.