Menu
Coddy logo textTech

Declaring an Array

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 57 of 77.

An array is a collection of items, and it can contain values of different types, such as numbers, strings, or even other arrays. Arrays are created using square brackets [], and the items inside the array are separated with commas.

Here is an example of how to create an array:

let myArray = [1, 2, "three", true];

To check the length of the array, we can use the length property:

let length = myArray.length;

The variable length will hold 4 because there are 4 elements in the array.

challenge icon

Challenge

Easy

Create an array called shoppingList that contains the following items: bread, eggs, milk, and butter.

Output the array in the end.

Cheat sheet

Arrays are collections of items created using square brackets [] with items separated by commas:

let myArray = [1, 2, "three", true];

To get the length of an array, use the length property:

let length = myArray.length;

Try it yourself

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals