Question
How do I add a value to the end of an array?
Answer
We can use the .push()
method to add an item (or multiple items) to the end of an array.
Here’s how we use the .push()
method:
Syntax:
array.push(item1, item2, item3, ...);
Example:
let codingLanguages = ['javascript', 'python', 'c', 'java'];
codingLanguages.push('ruby');
console.log(codingLanguages); //will log `[ 'javascript', 'python', 'c', 'java', 'ruby' ]` to the console