Material Box

Material Box

WEB Design & Material Images

JavaScript - Add value to array

JavaScript

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;

JavaScript - Add value to array

TitleJavaScript - Add value to array

CategoryJavaScript

Created

Update

AuthorYousuke.U