JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page

Immutability

Immutability refers to the concept of not being able to change the value of an object or variable once it has been created. This means that, once you have created an object or variable, you cannot modify its value directly. Instead, you must create a new object or variable with the modified value.

There are a few ways you can achieve immutability in JavaScript:

  • Using the Object.freeze() method: This method freezes an object, which means that you cannot add, delete, or modify any of its properties. However, you can still access the properties of a frozen object.

const obj = {
  name: 'John',
  age: 30
};

Object.freeze(obj);

obj.name = 'Sara'; // This will have no effect, because the object is frozen
console.log(obj.name); // 'John'

Using the Object.assign() method: This method creates a new object and copies the properties of an existing object into it. You can use it to create a modified version of an object without changing the original object.

const obj = {
  name: 'John',
  age: 30
};

const updatedObj = Object.assign({}, obj, { age: 31 });

console.log(obj.age); // 30
console.log(updatedObj.age); // 31

Using the spread operator (...): This operator allows you to create a new array or object that includes the values of an existing array or object. You can use it to create a modified version of an array or object without changing the original.

const arr = [1, 2, 3];

const updatedArr = [...arr, 4];

console.log(arr); // [1, 2, 3]
console.log(updatedArr); // [1, 2, 3, 4]

Using immutability in your code has several benefits, including:

  • Simplifying debugging: If you are not able to change the value of an object or variable, you can be sure that its value has not been modified unexpectedly. This can make it easier to identify the cause of an error or bug.

  • Improving performance: If you create a new object or array each time you need to modify an existing one, you may see a performance boost because JavaScript's garbage collection process will be able to clean up the old objects and arrays more efficiently.

  • Enabling functional programming: By not modifying the value of an object or variable, you can avoid side effects and create pure functions that are easier to test and reason about.

PreviousPlatformer ExampleNextPuzzle game example

Last updated 2 years ago

🍈