Learn

One way we can create an array is to use an array literal. An array literal creates an array by wrapping items in square brackets []. Remember from the previous exercise, arrays can store any data type — we can have an array that holds all the same data types or an array that holds different data types.

Diagram outlining an array literal that has 3 separate elements, a comma separates each element (a string, a number, and a boolean) and the elements are wrapped with square brackets

Let’s take a closer look at the syntax in the array example:

  • The array is represented by the square brackets [] and the content inside.
  • Each content item inside an array is called an element.
  • There are three different elements inside the array.
  • Each element inside the array is a different data type.

We can also save an array to a variable. You may have noticed we did this in the previous exercise:

let newYearsResolutions = ['Keep a journal', 'Take a falconry class', 'Learn to juggle'];

Let’s practice by making an array of our own.

Instructions

1.

Declare a variable using const named hobbies and set it equal to an array with three strings inside of it.

2.

Use console.log() to print hobbies to the console.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?