Take a look at the code below:
- Added “Honda” and “Toyota” onto the end of our array by using the push() method.
- Finally, we logged our array to the browser’s console.
If you run the example above, you will see that the elements “Honda” and “Toyota” have been successfully added onto the end of our array.
Note that the push() method will return the new length of the array:
//Add another element. var length = cars.push('Mercedes'); console.log('The array now contains ' + length + ' elements.');
i.e. If you add an element onto an array that originally had 3 elements, then the push() method will return 4.
You can also use the push() method to append multiple elements in one line of code:
//Adding multiple elements. cars.push('Mercedes', 'Honda', 'Toyota');
Hopefully, you found this tutorial to be helpful!