JavaScript number combination and case explanation

  • 2021-11-10 08:50:52
  • OfStack

Method 1:


var a = [1,2,3];
var b=[4,5]
a = a.concat(b);
console.log(a);
// The output here is  [1, 2, 3 ,4 ,5]

Method 2:


// ES5  The writing style of 
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
Array.prototype.push.apply(arr1, arr2);
console.log(arr1)//[0,1,2,3,4,5]

Method 3:


// ES6  The writing style of 
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1.push(...arr2);
console.log(arr1)//[0,1,2,3,4,5]

// ES6  The writing style of 
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
var arr3=[...arr1, ...arr2]
console.log(arr3)//[0,1,2,3,4,5]

Related articles: