An Array is a data structure that can store multiple values in a single variable.
In JavaScript, arrays are created using square brackets [] and can store:
- Numbers
- Strings
- Boolean values
- Objects
- Arrays
- Mixed Data Types
Arrays are Zero Indexed, which means:
- First element → Index 0
- Second element → Index 1
- Third element → Index 2
and so on.
var numbers = [1, 2, 3, 4, 5];
console.log(numbers);
console.log(typeof(numbers));[1, 2, 3, 4, 5]
object
numbersis an array containing five numeric values.- Arrays are special types of objects in JavaScript.
- Therefore:
typeof(numbers)returns
object
JavaScript arrays can store different types of values.
var mixedArray = [
1,
"hello",
true,
{name : "John"},
[1, 2, 3]
];
console.log(mixedArray);
console.log(typeof(mixedArray));[
1,
"hello",
true,
{name:"John"},
[1,2,3]
]
object
JavaScript provides many built-in methods to manipulate arrays.
Some important methods are:
- push()
- pop()
- unshift()
- shift()
- slice()
- splice()
- delete
The push() method adds one or more elements at the end of the array.
var fruits = ["apple", "banana", "orange"];
fruits.push("grape");
console.log(fruits);["apple", "banana", "orange", "grape"]
The pop() method removes the last element from the array.
fruits.pop();
console.log(fruits);["apple", "banana", "orange"]
The unshift() method adds elements at the beginning of the array.
fruits.unshift("kiwi");
console.log(fruits);["kiwi", "apple", "banana", "orange"]
The shift() method removes the first element from the array.
fruits.shift();
console.log(fruits);["apple", "banana", "orange"]
The slice() method creates a new array from the selected portion of an existing array.
array.slice(startIndex, endIndex)Note:
- Start Index → Included
- End Index → Excluded
var slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits);["banana", "orange"]
The splice() method is used to:
- Add elements
- Remove elements
- Replace elements
fruits.splice(1, 1, "grapefruit");
console.log(fruits);["apple", "grapefruit", "orange"]
splice(1,1,"grapefruit")means:
- Start from index 1
- Remove 1 element
- Insert "grapefruit"
The delete operator removes the value at a specific index.
delete fruits[2];
console.log(fruits);["apple", "grapefruit", empty]
or
["apple", "grapefruit", undefined]
The delete operator:
- Does not change array length.
- Only removes the value.
- Creates an empty slot.
fruits[2] = "melon";
console.log(fruits);["apple", "grapefruit", "melon"]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arrays in JavaScript</title>
</head>
<body>
<h1 id="data"></h1>
<script src="Arrays.js"></script>
</body>
</html>var ar = [1, 2, 3, 4, 5];
document.getElementById("data").innerHTML = ar;Returns the number of elements in the array.
Example:
var ar = [10, 20, 30, 40];
console.log(ar.length);Output:
4
- Arrays are zero indexed.
- Arrays can store different data types.
- Arrays are special types of Objects.
typeof(array)returns"object".push()adds elements at the end.pop()removes the last element.unshift()adds elements at the beginning.shift()removes the first element.slice()creates a new array.splice()modifies the original array.deleteremoves the value but not the index.
Arrays are one of the most important data structures in JavaScript.
They are used to:
- Store multiple values
- Manage collections of data
- Process lists of records
- Manipulate dynamic data efficiently
JavaScript provides many built-in methods such as:
- push()
- pop()
- shift()
- unshift()
- slice()
- splice()
Mastering arrays is essential for learning advanced JavaScript concepts like Objects, DOM Manipulation, ES6 Features, and Frameworks such as React and Angular.