In the above example, we accessed the array at position 1 of the nestedArray variable, then the item at position 0 in the inner array. Hence in order to add an element in the array, one of the following methods can be done: By creating a new array: Create a new array of size n+1, where n is the size of the original array. First we invoke unshift passing a single argument, then multiple arguments, displaying the results using console.log: The array splice method can be used for adding and/or removing elements from an array. CONCAT FUNCTION. The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable. The splice() method. You can add elements to the end of an array using push, … When using splice to add elements to an array, the second argument would be zero. This is because when you use methods of the Array object such as array.shift() or array.unshift(), each element’s index changes. Where the original array will remain untouched and a new array will contain the addition. Code:
You can click the button to add a new subject mathematics in the subjects array.