add to aray in javascript

You can add elements to an array in JavaScript using the push() method, the spread operator (...), or by directly indexing the array.

Using the push() method:

index.tsx
let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // [1, 2, 3, 4]
68 chars
4 lines

Using the spread operator:

index.tsx
let arr = [1, 2, 3];
arr = [...arr, 4];
console.log(arr); // [1, 2, 3, 4]
74 chars
4 lines

Directly indexing the array:

index.tsx
let arr = [1, 2, 3];
arr[arr.length] = 4;
console.log(arr); // [1, 2, 3, 4]
76 chars
4 lines

gistlibby LogSnag