Introduction
In JavaScript, an array is a special type of object used to store multiple values in a single variable. Arrays are ordered collections, meaning the values (called elements) are stored in a sequence, and each element can be accessed by its numerical index.
OR
An array is used to store more than one value in a single variable.
What is a JavaScript Array?
A JavaScript Array is a special variable used to store multiple values in a single variable.
Syntax of JavaScript Array
Basic syntax:
let arrayName = [value1,value2,value3];
Example:
let cities =["Delhi","Mumbai","Noida"];
Why JavaScript Arrays are Used?
There are many reasons to use JavaScript Arrays
- Store Multiple Values
- Easy Data Management
- Loop Through Data
- Useful in APIs
- Required in Modern JavaScript
Creating Arrays in JavaScript
There are many ways to create an array in JavaScript.
Method 1: Using Square Brackets
This is the most common method to create a Array.
let fruits = ["Apple","Banana","Mango"];
Method 2: Using Array Constructor
let colors = new Array("Red","Blue","Green");
Method 3: Empty Array
let users = [];
// now you can add name through push method
users.push("John");
Accessing Array Elements
Arrays use indexes to store values, and the index starts at 0.
Example:
let fruits = ["Apple","Banana","Mango"];
console.log(fruits[0]);
Output:
Access the second element:
let fruits = ["Apple","Banana","Mango"];
console.log(fruits[1]);
Output:
Updating Array Values
Suppose you have to update the value from Red to Black.
let colors = ["Red","Blue"];
colors[0]="Black";
console.log(colors);
Output:
Finding Array Length
to get the array length through the method below
Syntax:
arrayName.length
Example:
let fruits = ["Apple","Banana","Mango"];
console.log(fruits.length);
Output:
JavaScript Array Methods
You will see some JavaScript Array methods that are most important for the project development.
1. push()
This method is used to add an element to the end of the Array element.
users=["John","Tom"];
users.push("Rom");
console.log(users);
Output:
2. pop()
This method removes the last element.
users=["John","Tom"];
users.pop("Rom");
console.log(users);
Output:
3. shift()
This method removes the first element.
let numbers = [1,2,3];
numbers.shift();
console.log(numbers);
Output:
4. unshift()
This method is used to add at the beginning.
let nums = [2,3];
nums.unshift(1);
console.log(nums);
Output:
5. includes()
This method is used to check whether the value exists or not.
let fruits = ["Apple","Banana"];
console.log(fruits.includes("Apple"));
Output:
6. indexOf()
This method is used to find the position.
let fruits = ["Apple","Banana"];
console.log(fruits.indexOf("Banana"));
Output:
7. slice()
This method is used to extract part of an array.
let nums = [1,2,3,4];
console.log(nums.slice(1,3));
Output:
8. splice()
This method is used to add/remove elements.
let arr = [1,2,3];
arr.splice(1,1);
console.log(arr);
Output:
Explanation:
1(startIndex) → Start at index1(value2)1(deleteCount) → Remove 1 element
Using for Loop Through Arrays
let fruits = ["Apple","Banana"];
for(let i=0; i<fruits.length; i++)
{
console.log(fruits[i]);
}
Output:
Banana
Multidimensional Arrays
Array inside array.
let data = [
["John",35],
["Tom",30]
];
console.log(data[0][0]);
Output:
Real-Life Example 1: Todo List
let todos = ["Study","Exercise","Coding"];
todos.push("Sleep");
console.log(todos);
Output:
Real-Life Example 2: Get the total Marks of Student
let marks = [90,85,70];
let total = marks[0] + marks[1] + marks[2];
console.log(total);
Output:
Common Mistakes in Arrays
Mistake 1: Access Wrong Index
Wrong:
let arr=[1,2];
console.log(arr[5]);
Output:
Mistake 2: Using () Instead of []
Wrong:
arr(0)
Correct:
arr[0]
Difference Between Array and Object
| Feature | Array | Object |
|---|---|---|
| Storage | Multiple values | Key-value pairs |
| Index | Numeric | Named keys |
| Syntax | [] | {} |
| Example | ["HTML","CSS"] |
{
|
JavaScript Array – Interview Questions
Q 1: What is an array?
Q 2: How to create an array?
Q 3: Are arrays zero-indexed?
Q 4: Can arrays store mixed types?
Q 5: How to find array length?
JavaScript Array – Objective Questions (MCQs)
Q1. Which symbol is used to define an array?
Q2. Array index starts from ______.
Q3. Which method adds an element at the end of an array?
Q4. Which method removes the last element?
Q5. JavaScript arrays are ______.
Conclusion
JavaScript arrays are essential for storing and managing multiple values efficiently. They simplify data handling, improve code readability, and are widely used in web applications, APIs, and frameworks.
Understanding arrays and methods like push(), pop(), slice(), and splice() helps build stronger JavaScript fundamentals.