An array is a special variable that can hold multiple values under a single name.
-
Arrays can hold numbers, strings, objects, functions, or even other arrays.
-
Arrays are zero-indexed, meaning the first element is at index
0.
Example:
let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits[0]); // Apple
console.log(fruits[2]); // Orange
2. Creating Arrays
a) Using square brackets [] (most common)
let numbers = [1, 2, 3, 4, 5];
b) Using Array constructor
let colors = new Array("Red", "Green", "Blue");
c) Creating an empty array
let emptyArr = [];
3. Accessing Array Elements
-
Use index to access elements.
let arr = ["A", "B", "C"];
console.log(arr[0]); // A
console.log(arr[2]); // C
-
You can also modify elements using the index:
arr[1] = "Z";
console.log(arr); // ["A", "Z", "C"]
4. Array Properties
a) .length
Returns the number of elements in the array.
let arr = [1, 2, 3];
console.log(arr.length); // 3
5. Adding & Removing Elements
a) Add elements
-
push()→ add at the end -
unshift()→ add at the beginning
let arr = [1, 2];
arr.push(3); // [1,2,3]
arr.unshift(0); // [0,1,2,3]
b) Remove elements
-
pop()→ remove from the end -
shift()→ remove from the beginning
arr.pop(); // [0,1,2]
arr.shift(); // [1,2]
c) Remove/replace using splice()
let arr = [1,2,3,4];
arr.splice(1,2); // remove 2 elements starting from index 1 → [1,4]
arr.splice(1,1,9,8); // replace 1 element at index 1 with 9,8 → [1,9,8,4]
6. Array Methods
Here are the most commonly used:
| Method | Description | Example |
|---|---|---|
concat() | Merge arrays | [1,2].concat([3,4]) → [1,2,3,4] |
join() | Convert to string | [1,2,3].join("-") → "1-2-3" |
slice() | Copy a part | [1,2,3,4].slice(1,3) → [2,3] |
indexOf() | Find index | [1,2,3].indexOf(2) → 1 |
includes() | Check if exists | [1,2,3].includes(2) → true |
reverse() | Reverse array | [1,2,3].reverse() → [3,2,1] |
sort() | Sort array | [3,1,2].sort() → [1,2,3] |
forEach() | Loop over items | [1,2,3].forEach(n => console.log(n)) |
map() | Transform array | [1,2,3].map(n => n*2) → [2,4,6] |
filter() | Filter elements | [1,2,3,4].filter(n => n>2) → [3,4] |
reduce() | Reduce to single value | [1,2,3].reduce((a,b)=>a+b) → 6 |
7. Multi-Dimensional Arrays
Arrays can contain other arrays:
let matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
];
console.log(matrix[1][2]); // 6
8. Checking Arrays
-
Use
Array.isArray()to check if a variable is an array:
let arr = [1,2,3];
console.log(Array.isArray(arr)); // true
console.log(Array.isArray(123)); // false
9. Array Destructuring
let arr = [1,2,3];
let [a, b, c] = arr;
console.log(a, b, c); // 1 2 3
10. Spread Operator with Arrays
-
Expand array elements:
let arr1 = [1,2];
let arr2 = [3,4];
let arr3 = [...arr1, ...arr2]; // [1,2,3,4]
11. Common Tricks
-
Copy array:
let copy = [...arr]; -
Flatten array:
arr.flat() -
Find max/min:
Math.max(...arr)/Math.min(...arr)
12. Array Iteration
let fruits = ["Apple","Banana","Cherry"];
// for loop
for(let i=0; i<fruits.length; i++){
console.log(fruits[i]);
}
// for..of loop
for(let fruit of fruits){
console.log(fruit);
}
// forEach method
fruits.forEach(fruit => console.log(fruit));
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment