Primitive and Reference Values - Java Script

In ECMAScript, a variable can hold one of two types of values:primitive values and reference values.

  • Primitive values are simple pieces of data that are stored on the stack, which is to say that their value is stored directly in the location that the variable accesses.
  • Reference values,on the other hand, are objects that are stored in the heap, meaning that the value stored in the variable location is a pointer to a location in memory where the object is stored.

When a value is assigned to a variable, the ECMAScript interpreter must decide if it is a primitive or reference value. To do this, the interpreter tries to determine if the value is one of the ECMAScript primitive types:Undefined, Null, Boolean, Number, or String. Because each one of these primitive types takes up a fixed amount of space, it can be stored in the small memory area known as the stack. Doing so allows for quick look up of variable values.

  • In image laguages, strings are considered as reference type and not a primitive type because a string can vary in length. ECMAScript breaks from this tradition

If the value is a reference, then space is allocated on the heap. Because a reference value’s size can vary, it cannot be placed on the stack because it would reduce the speed of variable lookup. Instead, the value placed in the variable’s stack space is an address of a location in the heap where the object is stored. This address does have a fixed size; so storing it in the stack has no negative effect on variable performance.

Primitive and Reference Values

If you use a reserved word as a variable or function name, more than likely you will not receive an error...until a future browser implements one of them.Then the word will be considered a keyword, and you will get a keyword error.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics