Skip to main content

Command Palette

Search for a command to run...

Array method

Updated
3 min readView as Markdown
Array method
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

i hope sice you are reading this you must know about arrays. JavaScript provides powerful built-in array methods that make your code shorter, readable, and professional.

Let’s understand the most important ones with simple examples.

🔹 1. push()

Add a new moment at the end.

let moments = ["Cuddling", "teasing", "Swimming"];

moments.push("Talking");

console.log(moments);

Result:

["Cuddling", "teasing", "Swimming", "Talking"]

🔹 2. pop()

Removes the last element from an array.
It modifies the original array.

Example:

let moments = ["Cuddling", "teasing", "Swimming"];

moments.pop();

console.log(moments);

Before:

["Cuddling", "teasing", "Swimming"]

After:

["Cuddling", "teasing"]

🔹 3. shift()

Removes the first element of an array.

Example:

let moments = ["Cuddling", "teasing", "Swimming"];

moments.shift();

console.log(moments);

After:

["teasing", "Swimming"]

🔹 4. unshift()

Adds element to the beginning of an array.

Example:

let moments = ["Cuddling", "teasing", "Swimming"];

moments.unshift("Hugging");

console.log(moments);

After:

["Hugging", "Cuddling", "teasing", "Swimming"]

🔹 5. forEach()

Loops through each element.

Used for iteration only.

Does NOT return a new array.

Example:

let moments = ["Cuddling", "teasing", "Swimming"];

moments.forEach(function(moment) {
  console.log(moment);
});

Output:

Cuddling
teasing
Swimming

Use case: Printing, logging, performing actions.


🔹 6. map()

Creates a new array by transforming each element.

Does NOT change original array.

Example:

Convert to uppercase.

let moments = ["Cuddling", "teasing", "Swimming"];

let upperMoments = moments.map(function(moment) {
  return moment.toUpperCase();
});

console.log(upperMoments);

Output:

["CUDDLING", "TEASING", "SWIMMING"]

Original array remains unchanged. that is why mao is used in function programming


🔹 7. filter()

Creates a new array with elements that satisfy a condition.

Example:

Get moments that contain letter "i".

let moments = ["Cuddling", "teasing", "Swimming"];

let result = moments.filter(function(moment) {
  return moment.includes("i");
});

console.log(result);

Output:

["Swimming"]

Only matching values are kept.


🔹 8. reduce()

Reduces an array to a single value.

Example:

Combine all moments into one sentence.

let moments = ["Cuddling", "teasing", "Swimming"];

let sentence = moments.reduce(function(acc, moment) {
  return acc + " " + moment;
});

console.log(sentence);

Output:

Cuddling teasing Swimming

Reduce works by:

  • Taking an accumulator

  • Processing each element

  • Returning one final result


Comparison

Method Returns New Array? Changes Original?
push No Yes
pop No Yes
shift No Yes
unshift No Yes
forEach No No
map Yes No
filter Yes No
reduce Single value No