Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 5 - Basic Data Structures

5.1 Arrays

An array is a collection of elements that can hold data of the same or different types.

Imagine you are organizing a small party and you want to keep a list of the names of your guests. This list of guest names is a collection programmatically known as an array.

In JavaScript, an array is declared using square brackets [].

let fruits = ['Apple', 'Banana', 'Orange'];

To access a specific item from the fruits array, call the items by their index (position). Remember that the array items' index starts from 0, not 1.

Follow the example below:

let fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits[2]); // Accessing the item at index 2

Try to change the index of fruits inside the console.log() function.

Similarly, change specific array items by using their index as follows:

let fruits = ['Apple', 'Banana', 'Orange'];
fruits[1] = 'Mango';
console.log(fruits);

The line fruits[1] = 'Mango' changes the item at index 1.