Arrays

An Array stores multiple values in a single variable.

let fruits = ["Apple", "Orange", "Mango"];

Access Elements

console.log(fruits[0]);
console.log(fruits[1]);

Output

Apple
Orange

Change Value

fruits[1] = "Banana";

console.log(fruits);

Array Length

console.log(fruits.length);

Output

3


Basic Array Methods

push()

Add item at end.

let arr = [1, 2, 3];

arr.push(4);

console.log(arr);

Output

[1,2,3,4]


pop()

Remove last item.

arr.pop();

console.log(arr);

Output

[1,2,3]


unshift()

Add at beginning.

arr.unshift(0);

console.log(arr);

Output

[0,1,2,3]


shift()

Remove first element.

arr.shift();

console.log(arr);

Output

[1,2,3]


1. forEach()

What is it?

forEach() is used to execute a function for every element in an array.

Think of it as:

"Go through each item and do something."


Syntax

array.forEach((element, index, array) => {
    // code
});


Example

numbers.forEach(num => {
    console.log(num);
});

Output

1
2
3
4
5


Example with Index

numbers.forEach((num, index) => {
    console.log(index, num);
});

Output

0 1
1 2
2 3
3 4
4 5


Important

const result = numbers.forEach(num => num * 2);

console.log(result);

Output:

undefined

❌ forEach() does NOT return anything.


2. map()

What is it?

map() creates a NEW array by transforming each element.

Think:

"Take every item and convert it into something else."

Syntax

array.map((element) => {
    return something;
});

Example

const doubled = numbers.map(num => num * 2);

console.log(doubled);

Output:

[2, 4, 6, 8, 10]

Original array:

[1,2,3,4,5]

New array:

[2,4,6,8,10]


3. filter()

What is it?

filter() returns a NEW array containing only elements that pass a condition.

Think:

"Keep only the items that match."

Syntax

array.filter(element => condition);

Example

const even = numbers.filter(
    num => num % 2 === 0
);

console.log(even);

Output:

[2,4]


Real World Example

const users = [
    {name:"Jithu", age:22},
    {name:"Rahul", age:17},
    {name:"Arun", age:25}
];

const adults = users.filter(
    user => user.age >= 18
);

console.log(adults);

Output:

[
 {name:"Jithu", age:22},
 {name:"Arun", age:25}
]


4. find()

What is it?

find() returns ONLY the first element that matches a condition.

Think:

"Find the first matching item and stop."

Example

const result = numbers.find(
    num => num > 3
);

console.log(result);

Output:

4


5. some()

What is it?

Checks whether AT LEAST ONE element satisfies a condition.

Think:

"Does any item match?"

Example

const result = numbers.some(
    num => num > 4
);

console.log(result);

Output:

true

Because 5 is greater than 4.


Example

const result = numbers.some(
    num => num > 10
);

console.log(result);

Output:

false


6. every()

What is it?

Checks if ALL elements satisfy a condition.

Think:

"Do all items match?"

Example

const result = numbers.every(
    num => num > 0
);

console.log(result);

Output:

true

Example

const result = numbers.every(
    num => num > 2
);

console.log(result);

Output:

false


7. reduce()

What is it?

reduce() converts an entire array into a single value.

Think:

"Reduce many values into one."

Syntax

array.reduce(
    (accumulator, currentValue) => {},
    initialValue
);

Example: Sum

const total = numbers.reduce(
    (acc, curr) => acc + curr,
    0
);

console.log(total);

Output:

15

Step-by-Step

[1,2,3,4,5]

Initial:

acc = 0

Iteration 1:

0 + 1 = 1

Iteration 2:

1 + 2 = 3

Iteration 3:

3 + 3 = 6

Iteration 4:

6 + 4 = 10

Iteration 5:

10 + 5 = 15

Final:

15

Real World Example

Calculate total cart price.

const cart = [
    {name:"Phone", price:30000},
    {name:"Mouse", price:500},
    {name:"Keyboard", price:2000}
];

const total = cart.reduce(
    (sum, item) => sum + item.price,
    0
);

console.log(total);

Output:

32500


8. sort()

What is it?

Used to arrange array elements.

Think:

"Put items in order."

Sorting Strings

const fruits = [
    "Orange",
    "Apple",
    "Banana"
];

fruits.sort();

console.log(fruits);

Output:

["Apple","Banana","Orange"]

Sorting Numbers

Wrong Way

const nums = [100, 5, 20, 1];

nums.sort();

console.log(nums);

Output:

[1,100,20,5]

Because JavaScript treats them as strings.

Correct Way

nums.sort((a,b) => a - b);

Output:

[1,5,20,100]

How It Works

(a,b) => a - b

If result is:

Negative → a comes first
Positive → b comes first
0 → no change

Descending Order

nums.sort((a,b) => b - a);

Output:

[100,20,5,1]


Sort by Age

const users = [
    { name: "Jithu", age: 22 },
    { name: "Rahul", age: 18 },
    { name: "Arun", age: 25 }
];

users.sort((a, b) => a.age - b.age);

console.log(users);

Output:

[
 {name:"Rahul", age:18},
 {name:"Jithu", age:22},
 {name:"Arun", age:25}
]