Skip to content

Arrays

In JavaScript, an array is a type of variable used to store sequences of values or a series of objects. Declare an array using square brackets, with items separated by commas.

const quickArray = [12, 34, 56, 78];

To access a single value in the array, use bracket notation with the index number, which indicates the item's position within the array, starting at 0. For example, given quickArray above:

quickArray[0];

would return the first value, 12.

An array can be populated with other variables.

const x = 10;
const y = 11;
const z = 12;

const coordinates = [x, y, z];

When writing objects directly into an array, ensure that square and curly brackets are properly organized.

const classes = [
  {
    courseNo: "IMS322",
    credits: 3
  },
  {
    courseNo: "MUS100Z",
    credits: 1
  }
];

See the Pen Arrays (IMS322 Docs) by Eric Sheffield (@ersheff) on CodePen.

```