JavaScript for Beginners
About Lesson

An Array is a special variable that can hold more than one value at a time. Each value (also called an element) in an array has a numeric position, known as its index, and it can be individually referenced by that index.

 

How to Declare an Array

You can declare an array in JavaScript using the Array constructor or the array literal syntax.

// Using the Array constructor
let fruits = new Array("Apple", "Banana", "Mango");

// Using array literal syntax
let fruits = ["Apple", "Banana", "Mango"];

 

Accessing Array Elements

You can access the elements of an array by referring to the index number. Array indices start from 0, meaning the first element is at index 0.

let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Outputs: Apple

 

Modifying Array Elements

You can modify the elements of an array by using the index number.

let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Blueberry";
console.log(fruits); // Outputs: ["Apple", "Blueberry", "Mango"]

 

Array Properties and Methods

JavaScript arrays have many built-in properties and methods. For example, the length property returns the number of elements in the array, and the push() method adds a new element to the end of the array.

let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length); // Outputs: 3

fruits.push("Orange");
console.log(fruits); // Outputs: ["Apple", "Banana", "Mango", "Orange"]

 

Arrays are a powerful tool in JavaScript, allowing you to store and manipulate multiple values in a single variable. They are a fundamental concept that’s widely used in programming, so understanding how they work will greatly enhance your JavaScript coding skills.