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

javascript spread operator works on what structure

spread operator
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 :: js sort array 
Javascript :: quote 
Javascript :: for ... of ... 
Javascript :: split function in javascript 
Javascript :: cookie-parser get cookie 
Javascript :: delete icon 
Javascript :: javascript return function 
Javascript :: how to assign dynamic value to variable in javascript 
Javascript :: React Hook "useState" is called in function which is neither a React function component or a custom React Hook functio 
Javascript :: what is process.env.NODE_ENV 
Javascript :: jquery callback function example 
Javascript :: use index of an array within a for loop 
Javascript :: vuejs jwt authentication example 
Javascript :: nextjs markdown 
Javascript :: Template Literals for Strings 
Javascript :: firebase timestamp to date react 
Javascript :: comments in jsx 
Javascript :: what does find return javascript 
Javascript :: how to make and add to an array in javascript 
Javascript :: map duplicate keys JS 
Javascript :: props history 
Javascript :: html form action javascript method 
Javascript :: add 2 class names react 
Javascript :: async await map 
Javascript :: Requiring express 
Javascript :: javascript prototype inheritance 
Javascript :: chrome dev tools console api 
Javascript :: sumo multiselect 
Javascript :: query relation data in mongoose 
Javascript :: js match emoticon 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =