OneBite.Dev - Coding blog in a bite size

insert an element at the end of an array in Javascript

Code snippet on how to insert an element at the end of an array in Javascript

  let arr = [1, 2, 3]; 
  let endValue = 4; 
  arr.push(endValue);

This code adds an element to the end of an array. First, it declares an array, called “arr”, with the elements 1, 2, and 3. Then, it declares a variable called ‘endValue’ and assigns it to the value 4. Finally, it adds ‘endValue’ to the end of the array using the ‘push’ method. This method adds the element to the end of the array, and changes the array’s length accordingly. The new array is now [1, 2, 3, 4].

javascript