I have heard it said that JavaScript does not actually "point" to values (or objects, because everything is an object in JS) in memory as is the case in other languages. Rather, JS variables reference other values/objects in memory. Is this true? What is the semantic difference between pointing and referencing?
-
2http://programmers.stackexchange.com/questions/195337/is-there-any-difference-between-pointers-and-references – Felix Kling Jun 12 '16 at 01:14
-
You can't do pointer arithmetic on a reference variable. *"as is the case in other languages"* - Various other languages have "references" rather than "pointers". – nnnnnn Jun 12 '16 at 01:28
1 Answers
Variable names
JavaScript variable names are used as property name keys of an object holding variable values such as an environment record object of a function in scope of the code, or the global object itself. So variable names access an object property implementation of the variable's value and data type.
Under this model, yes, JavaScript values do not "point" to anything, they are property names of an object holding variable values.
Data Types
Everything an object in Javscript? No. Javascript contains primitive data types such as "number", "boolean", "null" etc. which can be represented by a limited number of octets in memory, a primiteve data type "string" which can use a variable amount of memory, and a complex data type "object" which can also use a variable amount of memory. Exactly how each data type is held in an object property and recalled for access is implementation dependent and outside the scope of this answer.
References, Pointers and JavaScript
As per the question linked in comments, pointers are a particular type of reference using memory address. More generally "reference" covers any value used to access another value, and could include memory address, table index or a key used to access something.
Object Data Type.
for completeness
Object data types are effectively a reference data type, containing a reference of some kind used by the Javascript engine to access data held for the object. Every equal valued copy of the same object reference accesses the same underlying object data structure.
Hence changes made to properties of an object passed as a parameter to a function persist upon return, and in the case of multiple variables set to the same object value, each variable accesses the same set of object properties.
-
Even primitive types like `Number` and `String` are objects. They may not be internally represented the same way as other types of objects, but they are still objects. (They have properties, a prototype, and so on.) – Raymond Chen Jun 12 '16 at 05:03
-
`Number` and `String` objects are wrappers around primitive data types. If such a primitive value is used as an object, however, the primitive type is converted to an instance of its wrapper object for the duration of the operation and no longer. Compare the value of `n.x` after `var n=1; n.x="x"` with `var n = new Number(1); n.x="x"` for example (neither throws an error). @RaymondChen – traktor Jun 12 '16 at 05:42
-
-
in regards to `"JavaScript variable names are used as property name keys of an object"`, can anyone please clarify `1)` what is this object? `2)` what properties does it have? i would like to be able to visualise it. i understand there are two value types (primitive and objects), and i understand that primitives have corresponding object wrappers so that methods can be called on them. but what i cannot understand yet is how an identifier (i.e. the 'name' of a variable) is the property of an object, and if so what is that object and what does it look like? thank you. – user1063287 Nov 12 '21 at 07:01
-
ps to last comment - i am looking at these two sites to try and visualise the structure of JavaScript variables 'under the hood', but am not sure if what they are displaying are abstracted models as opposed to the actual 'object model' of JavaScript: https://astexplorer.net and https://esprima.org/demo/parse.html . I can't yet figure out how to display this sort of object information with `console.log()` in the browser either. – user1063287 Nov 12 '21 at 07:29
-
@user1063287 Starting with [The Environment Record Type Hierarchy](https://tc39.es/ecma262/#sec-the-environment-record-type-hierarchy), the object holding variable names and values is a "declarative environment record" which is **not necesaarily** a JavaScript Object stored on the heap - it could be completely or partially on the stack, depending on implementation. You can most certainly can not access such records directly - the JavaScript engine, possibly written in C++ uses environment records to run code and find the values of source code identifiers. – traktor Nov 12 '21 at 11:33
-
ps. I doubt the parse tree will tell much about implementation. My impression is that the difference between primitive and object values is the first are passed on the call stack by value and the latter by a pointer to an object's data structure in heap memory. I did read years ago that JS engines have used [unused NAN values](https://stackoverflow.com/q/19800415/5217142) to pass non numeric values on the stack but can't quote where I read it. How and where primitive string text is stored remains a mystery to me. – traktor Nov 12 '21 at 11:51