Monday, April 27, 2020

JavaScript: Adding to arrays with "..."

In JavaScript, push method is used to add to an array. Let's say you have an existing array called out  = [{e1: 5}, {e2:10}] and want to add its elements to an array called arr. Results differ for arr.push(out) and arr.push(...out). When you use ellipsis (...), each element of out array will be added as a separate element to arr and arr.length will increase by 2 after push. Without ..., out will be added as a single element to arr, and arr.length will increase by 1 after push operation. Below is an example script:


No comments: