To include or add an element into an array, you can utilize the .splice() method.
By using .splice(), you have the flexibility to insert a new element at a specific index, either replacing the existing value or adding without overwriting.
For instance:
let secretMessage = ['Programming', 'is', 'not', 'about', 'what', 'you', 'get',
'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can',
'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'to', 'Program']
interpret: 'Programming is not about what you get easily the first time, it is about what you can figure out. -2015, Chris Pine, Learn to Program'
secretMessage.splice(6, 5, 'know,');
console.log(secretMessage.join(' '); //display array elements in console, joined with spaces
interpret: 'Programming is not about what you know, it is about what you can figure out. -2015, Chris Pine, Learn to Program'
In this demonstration, we replace the 5 elements from position 6 with 'know,'.
If you want to insert an element without removing another, specify 0 as the second argument in .splice() method.
For example:
let favDrink = ['I', 'like', 'milkshake.']
favDrink.splice(2, 0, 'banana'); //insert 'banana' at index 2, no replacement
console.log(favDrink.join(' ');
interpret: 'I like banana milkshake.'
Source:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice