Primitive values vs Reference values in JavaScript

In JavaScript, values are categorized into two types: primitive values and reference values. Understanding the difference between these two types is crucial for effectively managing and avoiding common pitfalls in JavaScript programming

Primitive Values

Primitive values are the most basic data types in JavaScript. They are immutable(values cannot be changed once created).

JavaScript has 6 primitive data types:

  • Number
  • String
  • Undefined
  • Null
  • Symbol
  • Boolean

Reference values

Reference values refer to objects. They are mutable(values can be changed after they are created). When you assign an object to a variable, you are assigning a reference to that object, not the object itself.

JavaScript has 4 reference values :

  • Object
  • Array
  • Function

Date

Key Differences

Immutability :

  • Primitive values are immutable. You cannot change the actual value but can reassign the variable.
  • Reference values are mutable. You can change the content of the object.

Memory:

  • Primitive values are stored directly in the variable.
  • Reference values store the reference (or address) to the actual object in memory.

Copying:

  • Primitive values: Copying a primitive value creates a new independent value.
  • Reference values: Copying a reference value only copies the reference, not the object itself.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top