JavaScript - Add value to array
A JavaScript code example for adding values to the beginning or end of an array.
The HTML has an output element with id "output" to display the result.
<p id="output"></p>
The push()
method can be used to add values to the end of an array.
The unshift()
method can be used to add values to the beginning of an array.
let array = ['Apple', 'Banana'];
// Add value to end of array
array.push('Grape');
// Add value to beginning of array
array.unshift('Orange');
console.log(array);
document.querySelector('#output').innerHTML = array;