JavaScript methods categories you must know🧡

Justine Peterson Mahinyila
2 min readOct 26, 2021

--

Well, we all agree that Javascript is King and Js methods are part of essential concepts you can hardly avoid using, the sooner and better you understand them the easier your debug life becomes. Let’s get to the chaseπŸ’ƒβ€¦

First thing first, there are two major categories of methods in JavaScript mutating and Iterating non-mutating methods

1. Mutating,

Mutating array methods will change the original array.

i. push()

The push() method as its name suggests inserts an element at the end of an array.

let animals = [🐈,🐈,🐈]animals.push(🐩)console.log(animals)
//Expected Output: [🐈,🐈,🐈,🐩]

ii. pop()

The pop method will remove the last element from the end of an array

let animals = [🐈,🐈,🐈,🐩]animals.pop()console.log(animals)// [🐈,🐈,🐈]

iii.unshift()

The unshift method will insert an element at the beginning

let animals = [🐈,🐈,🐈]animals.pop(🐩)console.log(animals)// [🐩,🐈,🐈,🐈]

iv.shift()

The shift method Remove first element(the zeroth index element) from an array

let animals = [🐩,🐈,🐈,🐈]animals.pop()console.log(animals)// [🐈,🐈,🐈]

v. reverse()

The reverses method will reverse an array, the first element becomes last and the last element becomes the first.

const array = [1️⃣, 2️⃣, 3️⃣];array.reverse()console.log(array)// [3️⃣, 2️⃣,1️⃣];

vi.sort()

The sort method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending.

const months = [β€˜March’, β€˜Jan’, β€˜Feb’, β€˜Dec’];months.sort();console.log(months);// expected output: Array [β€œDec”, β€œFeb”, β€œJan”, β€œMarch”]

2. Iterating

This is a group of methods that do not mutate the original array but iterates through the array.

i. forEach() calls the function provided once for each element

arr = [πŸ₯šπŸ₯šπŸ₯šπŸ₯š]
[πŸ₯šπŸ₯šπŸ₯šπŸ₯š].forEach((πŸ₯š)=>{
hatch(πŸ₯š)
})
//expected outputs
//🐣
//🐣
//🐣
//🐣
console.log(arr)//[πŸ₯šπŸ₯šπŸ₯šπŸ₯š]

ii. slice() this method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included)

const animals = [🐜, 🌺, 🐫, πŸ¦†, 🐘]console.log(animals.slice(2))// expected output: [🐫, πŸ¦†, 🐘]

iii. concat() this method appends one or more arrays with a given array, the method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

const g = [πŸ’ƒπŸ’ƒ πŸ’ƒ]const b = [πŸ•ΊπŸ•ΊπŸ•Ί]const bng = g.concat(b)console.log(bng)//[πŸ’ƒπŸ’ƒπŸ’ƒ, πŸ•ΊπŸ•ΊπŸ•Ί]

These are just a few examples of the array methods that you will often come across as you learn JavaScript, would you like to learn more? I will be sharing a lot of beginner-friendly content and insights on my Twitter timeline, you can follow me at @justinecodez

--

--

Justine Peterson Mahinyila
Justine Peterson Mahinyila

Written by Justine Peterson Mahinyila

My mission is to improve people’s lives and solve problems using technology in Africa

No responses yet