Spread operator in javascript

The spread operator essentially takes either an array or an object and expands it into its set of items.

The spread operator is denoted by three dots..

Const array = [1, 2];

const array2 = […array, 3, 4];

// The value of array2: [1, 2, 3, 4]

The spread operator is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.

Copying an array

let array1 = [“h’,  ‘e’,  ‘y’];

let array2 = [.. .array1] ;

console.log(array2);

// array2: [‘h’, ‘e’, ‘y’]

Inserting the elements of one array into another

const array = [1, 2];

const array2 = […array, 3, 4];

// The value of array2: [1, 2, 3, 4]

The spread operator is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.

Copying an array

let array1 = [“h’,  ‘e’,  ‘y’];

let array2 = [.. .array1] ;

console.log(array2);

// array2: [‘h’, ‘e’, ‘y’]

Inserting the elements of one array into another

const array = [1, 2];

const array2 = […array, 3, 4];

// The value of array2: [1, 2, 3, 4]

Array to arguments

function multiply (number1, number2, number3) {

console. log(number1 * number2 * number3);

}

Leave a Reply

Your email address will not be published.