Advertisement
Google Ad Slot: content-top
JS Array
In JavaScript, an Array is a special variable used to store multiple values in a single variable. Arrays are dynamic, meaning their size can change, and they can hold different types of data.
Creating an Array:
- Using Array Literal (Preferred way)
- Using
new Array()Constructor - Empty Array Initialization
Using Array Litera Example
let fruits = ["Apple", "Banana", "Cherry"];
Using new Array() Constructor Example
let numbers = new Array(1, 2, 3, 4);
Empty Array Initialization Example
let emptyArray = [];
Accessing Array Elements:
Array elements are accessed using index numbers (0-based indexing).
Example
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana