Primitive Value VS Reference Value

Primitive value vs Reference value

A variable can hold one of two value types: primitive values or reference values.

  • Primitive values are data that are stored on the stack.
  • Primitive value is stored directly in the location that the variable accesses.
  • Reference values are objects that are stored in the heap.
  • Reference value stored in the variable location is a pointer to a location in memory where the object is stored.
  • Primitive types include Undefined, Null, Boolean, Number, or String.

The Basics:

Objects are aggregations of properties. A property can reference an object or a primitive. Primitives are values, they have no properties.

Updated:

JavaScript has 6 primitive data types: String, Number, Boolean, Null, Undefined, Symbol (new in ES6). With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.

What's the difference between primitive and reference types?

These are the primitive types in Java:

  • boolean
  • byte
  • short
  • char
  • int
  • long
  • float
  • double

All the other types are reference types: they reference objects.

This is the first part of the Java tutorial about the basics of the language.

Difference between primitive value and primitive type?

Hmm ... maybe you have a confusion with "primitive type". Most of the time "primitive type" (or primitive data type) and "primitive values" are used to speak the same things: boolean, null, undefined ...

But you can check these links to know more about it and find a better answer:

  • Official Primitive docs
  • Primitive values vs Reference values

Primitive and Reference data type in javascript

Assignment makes a copy of the value only if it's a primitive type (like Number, Boolean, etc...). Otherwise, assignment just copies a reference to the same object (Object, Array, etc...). A new object is not created with assignment.

Reference

An assignment does not create a copy/clone/duplicate of an object.

How exactly is does pass by value and pass by reference work in javascript?

When you declare variables, the JavaScript engine allocates the memory for them on two memory locations: stack and heap.

Static data is the data whose size is fixed at compile time. Static data includes:

  • Primitive values (null, undefined, boolean, number, string, symbol, and BigInt)
  • Reference values that refer to objects.

Because static data has a size that does not change, the JavaScript engine allocates a fixed amount of memory space to the static data and store it on the stack.

For example, the following declares two variables and initializes their values to a literal string and a number:

let name = 'John';
let age = 25;

Because name and age are primitive values, the JavaScript engine stores these variables on the stack as shown in the following picture:
Sample Image

Unlike the stack, JavaScript stores objects (and functions) on the heap.
The JavaScript engine doesn’t allocate a fixed amount of memory for these objects. Instead, it’ll allocate more space as needed.

The following example defines the name, age, and person variables:

let name = 'John';
let age = 25;

let person = {
name: 'John',
age: 25,
};

Sample Image

In this picture, JavaScript allocates memory on the stack for the three variables name, age, and person.

The JavaScript engine creates a new object on the heap memory. Also, it links the person variable on the stack memory to the object on the heap memory.

Because of this, we say that the person variable is a reference that refers to an object.

Please check here for details

Javascript closures: primitive vs reference behaviour

Primitive types contain the actual data, while reference types contain only a memory address (a pointer to the memory location where the object's data resides).

So when you want to check the length field of your array (which is a reference type) you first need to find what is the memory address where the object's data resides (you look into y variable), you go to that address and finally you look at the data.

Now, when you call a function, for each parameter, a copy of their value is made and is stored in a local variable, available in your function scope. So every time you pass your array as a parameter, a copy of the memory address where the data resides is stored in a local variable (only the memory address is copied, not the whole object).

So, to answer your question: no, primitive and reference types are not treated differently when passed to a function. A copy is made in both cases, except the primitives contain the actual data, while references do not, they contain an address (a pointer to the actual data). When you follow the address you get the data, but the data might have been modified between the time you made a copy of the address and the time you checked the data.



Related Topics



Leave a reply



Submit