Intro to JavaScript Arrays Fundamentals

Learning objective: By the end of this lesson, students will be able to create and initialize arrays using correct syntax and naming conventions.

Creating arrays

Use array literal notation to create an array using the following syntax:

A line of code reading: const nums = [2, 4, 18];. Elements of the code are numbered: 1) const, 2) nums, 3) the square brackets, 4) the numbers inside of the square brackets.

  1. The const keyword. Arrays should be declared using const.
  2. The name of the array should be plural.
  3. Opening and closing square brackets indicate an array.
  4. The array’s elements are placed inside the square brackets. A comma separates each element, meaning the above array has been initialized with three elements. You don’t have to include elements when initializing an array; use a set of empty square brackets - [].

🏆 The name of an array should always be plural because it holds a collection of items. The array above is named nums because it holds numbers inside of it.

An array can hold any data inside of it (including other arrays!) and can even hold multiple data types.

🎓 You Do

Create an array named movies containing the titles (as strings) of three of your favorite movies.

If you want your notes to match the lecture examples, use Barbie, Interstellar, and Get Out.