add an element to the end of an array in javascript

To add an element to the end of an array in JavaScript, you can use the push() method, as shown below:

index.tsx
let myArray = [1, 2, 3, 4]; // Declare and initialize an array
myArray.push(5); // Add an element to the end of the array
console.log(myArray); // Output: [1, 2, 3, 4, 5]
171 chars
4 lines

The push() method adds the specified element to the end of the array and returns the new length of the array. Note that this method modifies the original array and does not create a new one.

gistlibby LogSnag