Home  >  Article  >  Web Front-end  >  Spread operator in JavaScript

Spread operator in JavaScript

巴扎黑
巴扎黑Original
2017-07-21 17:06:451978browse

Expand operator (Spread operator)

let zzz=[2,4,6];
console.log(zzz);//[2, 4, 6]
console.log(...zzz);//2 4 6

Expand and merge

let a=[1,2,3];
let b=[...a,4,5,6];
console.log(b);//1,2,3,4,5,6

let da=[1,2];
let db=Array.from(da);
db.push(3);
console.log(da,db);//[1, 2]   [1, 2, 3]

concat’s connection array

var arr5= [3,4,5]
    var arr6 = [6,7,8]
    var arr7 = arr5.concat(arr6);
    console.log(arr7);//3,4,5,6,7,8
    var arr8 = [...arr5,...arr6];
    console.log(arr8);//3,4,5,6,7,8

Convert string into array

var str = 'html';
var arr9 = [...str];
console.log(arr9);//["h", "t", "m", "l"]

Application in destructuring assignment

let [a,b,...c]=[1,2,3,4,5];
console.log(a,b);//1 2
console.log(c);//[3, 4, 5]

var aa=[1,2,3];
var bb=[...aa];
bb.push(4,5);
console.log(bb);//[1, 2, 3, 4, 5]

Function call

function aa(a,b){
        return a+b
}
let cc=[1,3]
let bb=aa(...cc)
console.log(bb);//4

The above is the detailed content of Spread operator in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn