DEV Community

Discussion on: 6 Different Ways to Insert Elements to an Array in JavaScript

Collapse
 
lexlohr profile image
Alex Lohr

One seventh little known way is you can also use Object.assign on an array, even to add items beyond its original length:

let a = [1, 2, 3];
Object.assign(a, { 2: 4, 3: 8 });
console.log(a); // returns [1, 2, 4, 8]
Enter fullscreen mode Exit fullscreen mode