How to use Spread Operator In Javascript
The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected.
Syntax:
let variablename1 = [ ...value ];
Example: In this example, we will create a new object using the spread operator preserving the state of the old object.
Javascript
// Creating an object const oldObject = { name: 'John' , age: 30 }; // Creating new object from old one const newObject = { ...oldObject, age: 31 }; // Display properties of both objects console.log(oldObject); console.log(newObject); |
{ name: 'John', age: 30 } { name: 'John', age: 31 }
How to Preserve Immutability of Objects in JavaScript ?
Immutability is an issue when it comes to private or restricted objects. Sometimes we need to create new objects without making modifications or changes unintendedly to preserve the integrity of data. Thatâs why, we need to find a way to preserve the object states. In this article, we will understand the immutability of objects concept, along with understanding the different approaches to preserving the immutability of objects & correspondingly will know their basic implementation with the help of examples.
Contact Us