Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

spread operator in javascript

// spread operators can be used for arrays or objects

// cloning to prevent mutation.
let numList = [1,2,3];
let numListClone = [...numList]; // [1, 2, 3]

// spread operator for destructuring.
let animal = {
  name: 'dog',
  color: 'brown',
  age: 7
};
let { age, ...otherProperties } = animal; 

// spread operator as rest operator
function sum(x, y, ...rest) {}

// spread operator for merging arrays or objects
let numLists = [...numList1, ...numList2];
let animalWithBreed = {
  ...animal,
  breed: '',
}
Comment

array spread operator in javascript

let arr1 = ['A', 'B', 'C'];

let arr2 = ['X', 'Y', 'Z'];

let result = [...arr1, ...arr2];

console.log(result); // ['A', 'B', 'C', 'X', 'Y', 'Z']
// spread elements of the array instead of taking the array as a whole
Comment

javascript spread operator

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));
// expected output: 6
Comment

spread operator javascript

const parts = ['shoulders', 'knees']; 
const lyrics = ['head', ...parts, 'and', 'toes']; 
//  ["head", "shoulders", "knees", "and", "toes"]
Comment

Javascript spread operator

syntax:
...iterable
where, iterable is source like array, object, string etc 

const a=[1,2]
console.log(...a)
// prints 1 2

***spread operator makes a copy of source, not reference.
// ex. - if a & b are 2 arrays
a=[...b]
//changing a won't affect b & vice versa

*Also since it copies source, something like this is also possible:
const a=[5,1]
const b=[2]
const c=[...b,...a]
// c is [2,5,1]

*********************************************
For object just use {} instead of []
const a={ap:4}
const c={...a}

Comment

spread operator javascript

obj = {first_name : "Marty",
    lovers: ["Jennifer Parker","Baines McFly"]
      };
let objClone = { ...obj }; // pass all key:value pairs from an object 

console.log(objClone)
// { first_name: "Marty", lovers: ["Jennifer Parker", "Baines McFly"] }
Comment

javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99)
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
Comment

javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99)
//expected output: 88 2 4 6 8 10 99
Comment

Javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99) 
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
Comment

How javascript spread operator works?

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99) 
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
Comment

Javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99) 
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
Comment

spread operator javascript

//Spreed Operator
const price = [100, 150, 200, 300, 400];
const price2 = [500, 550, 600, 10];
const price3 = [35, 60, 70];

const allPrice = [...price, ...price2, ...price3];  //using three dots means spread operator
console.log(allPrice);  //100, 150, 200, 300, 400, 500, 550, 600, 10, 35, 60, 70]
Comment

javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99)
//expected output: 88 2 4 6 8 10 99
Comment

javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99)
//expected output: 88 2 4 6 8 10 99
Comment

javascript Spread Operator

const arrValue = ['My', 'name', 'is', 'Jack'];

console.log(arrValue);   // ["My", "name", "is", "Jack"]
console.log(...arrValue); // My name is Jack
Comment

Spread syntax in ES6

let arr1 = [1, 2, 3]
let func = (a, b, c) => a + b + c

console.log(func(...arr1)) // 6
Comment

es6 spread operator

spread operator uses
Comment

How to Use the Spread Operator in JavaScript

const newArray = ['firstItem', ...oldArray];
Comment

spread operator in javascript

let numberStore = [0, 1, 2];
let newNumber = 12;
numberStore = [...numberStore, newNumber];
hhh
Comment

spread operator es6

[...fruits]
Comment

javascript spread operator

var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99) 
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
Comment

PREVIOUS NEXT
Code Example
Javascript :: .remove javascript 
Javascript :: javascript sort object by value descending 
Javascript :: firebase.database.ServerValue.increment 
Javascript :: javascript array last element get 
Javascript :: nodejs debug 
Javascript :: convert string to a number javascript 
Javascript :: show uploaded image in react/nextjs 
Javascript :: null is true or false javascript 
Javascript :: spread operator react array 
Javascript :: js a function that takes in multiple arguments. 
Javascript :: Javascript print/output 
Javascript :: how to reload webview in react native 
Javascript :: Default Parameter Values in javascript 
Javascript :: reference data types in javascript 
Javascript :: javascript objet keys comparaison 
Javascript :: how to clear nodejs terminal in vs code 
Javascript :: break loop after time javascript 
Javascript :: call node.js file electron 
Javascript :: in express how do you set the location header 
Javascript :: datapack structure 
Javascript :: class keyword es6 
Javascript :: find element vs find elements 
Javascript :: convert javascript date into excel date 
Javascript :: How to put anything as log in console 
Javascript :: how to create a search engine with javascript 
Javascript :: 35,2 + 29,4 
Javascript :: last underscore 
Python :: python suppress warning 
Python :: converting string to datetime pandas 
Python :: how remove name of index pandas 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =