Skip to main content
Practice

Array

An array is an object that can store multiple values in a single variable. Each value in the array can be referenced by an index.


Creating an Array

Creating an array is simple. Just place the desired values inside square brackets [], separated by commas ,.

Creating an Array
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits); // ["Apple", "Banana", "Cherry"]

Accessing Array Values

To get an item within an array, you need to use the index (a number representing an element's position within the array). Array indices start at 0.

Accessing Array Values
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits[0]); // "Apple"
console.log(fruits[1]); // "Banana"

Finding the Length of an Array

You can find out how many items are in an array using the length property.

const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.length); // 3

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.