Growing Arrays
Add to an array with push.
1const fruits = ["apple", "banana"];
2fruits.push("cherry");
3console.log(fruits);
$ node main.js
[ 'apple', 'banana', 'cherry' ]
Every line, explained
const fruits = ["apple", "banana"];- An array stores several values in one variable. The values go between square brackets, separated by commas.
fruits.push("cherry");- .push(...) adds a new item to the END of the array. Surprise: fruits is a const, yet this works! const only locks the NAME to this one array; the array's contents can still change. What you can't do is fruits = somethingElse.
console.log(fruits);- console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot. The cherry is now in the array.