capypad
0 day streak
javascript / beginner
Snippet

Adding Items to an Array

Arrays are used to store lists of data. The '.push()' method adds a new element to the end of the array, and '.length' tells you how many items are inside.

snippet.js
javascript
1
2
3
const colors = ["red", "green"];
colors.push("blue");
console.log(colors.length);
Breakdown
1
["red", "green"]
Initializes an array with two string elements.
2
colors.push("blue");
Adds "blue" as the third element to the colors array.