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 :: script src= https//kit.fontawesome.com/a81368914c.js /script 
Javascript :: redux dev tool 
Javascript :: javascript play audio from buffer 
Javascript :: how to see if checkbox is checked 
Javascript :: import syntax node 
Javascript :: hourglasses js 
Javascript :: vue reset all data to default 
Javascript :: vuejs does props factory function have access to vue instance 
Javascript :: check if object is not defined js 
Javascript :: sequelize association helper methods 
Javascript :: short if statements in javascript 
Javascript :: next.js index page 
Javascript :: loop over an array 
Javascript :: angular timeout function 
Javascript :: ordering array 
Javascript :: how to show calendar in javascript 
Javascript :: javascript remove multiple commas from string 
Javascript :: js match any number string 
Javascript :: javascript array filter duplicates in react 
Javascript :: jquery select input with empty value 
Javascript :: how to check if the element exist in the parent element javascript 
Javascript :: upload files with angular 
Javascript :: cypress graphql request 
Javascript :: how to see if user on phone 
Javascript :: using underscore javascript number 
Javascript :: callback function js 
Javascript :: string splice javascript 
Javascript :: react native flexbox 2 columns 1 fixed width 
Javascript :: onclick arrow function javascript 
Javascript :: call apply and bind method in javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =