Immutable Primitive Values and Mutable Object References in Javascript

Primitive values in javascript are immutable (cannot be changed)

  • numbers
  • strings
  • Booleans

Non-primitive values in javascript are mutable (can be changed)

  • objects
  • Functions

  Non-primitive values are mutable

   The following is a list (array) of people assigned to a variable:

let person = ['John', 'Macmahon', 'Arnold', 'Ziglar']
person[0] = 'Schednegar'


// Output: Array ["Schednegar", "Macmahon", "Arnold", "Ziglar"]        

So, we can change the array elements from the array. So in javascript non-primitive values are mutable.

Primitive values are immutable

The following is a text (string) value that gets assigned to a variable

let person = 'John'
person[0] = 'A' 
console.log(person) // output: John        

This cannot be changed because the string is a primitive value. So primitive values are immutable means, not they cannot be changed.

Leave a Comment

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

Scroll to Top