The 2 methods to add an item to an array are unshift() and push(). The unshift() method inserts an item at the beginning of an array and the push() method inserts an item at the end of an array. We will use the push() method to achieve our goal.
In the following example, we have a global variable users and that holds an array. In the input element, we will enter some random text and upon click of a button, we will append it to the array. Please have a look over the code example and steps given below.
HTML & CSS
We have 4 elements in the HTML file (div, button, input, and ul). The div element is just a wrapper for the rest of the elements.
The inner text for the button element is “Add”.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.