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 syntax

let arr1 = ['one', 'two'];
let arr2 = [...arr1, 'three', 'four', 'five'];
console.log(arr2); // ["one", "two", "three", "four", "five"]
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

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

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 :: javascript problems 
Javascript :: search in array javascript 
Javascript :: access object property dynamically javascript 
Javascript :: js repeat 
Javascript :: push and unshift in javascript 
Javascript :: javascript get all options from select 
Javascript :: switch statement 
Javascript :: import module in ES6 
Javascript :: js array modify element 
Javascript :: javascript code for find the last element in array 
Javascript :: onmousedown 
Javascript :: react window navigate 
Javascript :: understanding currying 
Javascript :: javascript meme 
Javascript :: set timer 
Javascript :: js oop 
Javascript :: counting pairs in an array, resulting in a given sum 
Javascript :: fetch timeout 
Javascript :: use of slot in vue 
Javascript :: how to prevent previous radio button active react native 
Javascript :: giphy javascript github 
Javascript :: display rond logo in angular 
Javascript :: Cannot GET /assets/vendor/swiper/swiper-bundle.min.js.map 
Javascript :: simple if condition for form validation 
Javascript :: angular print an array 
Javascript :: javascript cargar un html 
Javascript :: JS urdsathdzygo8sdhurj.hdo78suij 
Javascript :: eeeeee 
Javascript :: add class to random element 
Javascript :: return $this-response-withType("application/json")-withStringBody(json_encode($result)); 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =