Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 2 - Basic JavaScript Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
Lesson 10 - User Input
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.