Skip to main content

Command Palette

Search for a command to run...

JavaScript Array Cheat Sheet

Published
•5 min read•View as Markdown
JavaScript Array Cheat Sheet
M

I am a passionate, athletic, full stack software developer . My aim is to make people's lives better & easier by developing applications and websites.

Hey there everyone, In this detailed blog, you will see and learn all the basic array methods and operations in JavaScript.

Basic Methods

1.concat

[1,2,3].concat([4,5])

Return Type : Array

Description : Concatenate the array passed to concat to the end of the array

Add the elements 4 and 5 to the end of the array

result : [1,2,3,4,5]

2.slice

[1,2,3,4].slice(1, 3)

Return Type : Array

Description : Copy part of an array starting at the index of the first parameter and stopping at the index of the second parameter, but not including the last index

Copy the values in index 1 and 2 into a new array

result : [2,3]

3.reverse

[1,2,3].reverse()

Return Type : Array(Modifies The Array)

Description : Reverse the order of the elements in an array

Reverse the order of the elements in an array

result : [3,2,1]

4.join

[1,2,3].join(“, “)

Return Type : String

Description : Combine each element of the array into a string with the value passed to join placed between each value

Add a comma and a space between each element in the array

result : 1, 2, 3

Array Manipulation

1.push

[1,2,3].push(4, 5)

Return Type : Number(Modifies The Array Return length of array)

Description : Add one or more elements to the end of the array

Add 4 and 5 to the end of the array

result : [1 ,2 ,3 ,4 ,5]

2.pop

[1,2,3].pop()

Return Type : Element(Modifies The Array Return removed element)

Description : Remove the last element from the array

Remove the value 3 from the array

result : [1 ,2]

3.shift

[1,2,3].shift()

Return Type : Element(Modifies The Array Return removed element)

Description : Remove the first element from the array

Remove the value 1 from the array

result : [2,3]

4.unshift

[1,2,3].unshift(4, 5)

Return Type : Number(Modifies The Array Return length of array)

Description : Add one or more elements to the start of the array

Add 4 and 5 to the start of the array

result : [4,5,1,2,3]

5.splice

[1,2,3].splice(1, 1, 4, 5)

Return Type : Number(Modifies The Array Return length of array)

Description : Starting at the index specified by the first parameter, remove the number of elements specified by the second parameter and then add elements to the array for all remaining parameters at the index specified by the first parameter

Starting at index 1, remove 1 element and then add the elements 4 and 5 at index 1.

result : [1,4,5,3]

Higher Order Functions

1.forEach

[1,2,3].forEach(n => console.log(n))

Return Type : Undefined

Description : Execute the function passed to forEach for each element in the array similar to a for loop

Log out each element in the array

result : None

2.map

[1,2,3].map(n => n * 2)

Return Type : Array

Description : Return a new array which contains the return result of running each item through the function passed to map

Create a new array with all values doubled

result : [2,4,6]

3.filter

[1,2,3].filter(n => n > 2)

Return Type : Array

Description : Return a new array which only contains values that return true to the function passed to filter

Create a new array with only values greater than 2

result : [3]

4.find

[1,2,3].find(n => n > 2)

Return Type : Element

Description : Select the first element that returns true to the function passed to find

Get the first element greater than 1

result : 2

5.findIndex

[1,2,3].findIndex(n => n > 1)

Return Type : Number

Description : Get the index of the first element that returns true to the function passed to find

Get the index of the first element greater than 1

result : 1

6.every

[1,2,3].every(n => n > 1)

Return Type : Boolean

Description : Return true if every element in the array returns true from the function passed to every

Check if every element is greater than 1

result : false

7.some

[1,2,3].some(n => n > 1)

Return Type : Boolean

Description : Return true if at least one element in the array returns true from the function passed to some

Check if any element is greater than 1

result : true

8.reduce

[1,2,3].reduce((sum, n) => sum + n, 0)

Return Type :Any Value

Description : Reduce the array to one single value by starting the sum at the second value passed to reduce and updating the sum with the return value of each iteration

Sum all the numbers in the array starting with 0

result : 6

Basic Value Check

1.indexOf

[1,2,1].indexOf(1)

Return Type :Number

Description : Return the first index of the value passed to indexOf or return -1 if the value cannot be found

Get the first index of 1 in the array

result : 0

2.lastIndexOf

[1,2,1].lastIndexOf(1)

Return Type :Number

Description : Return the last index of the value passed to indexOf or return -1 if the value cannot be found

Get the last index of 1 in the array

result : 2

3.includes

[1,2,3].includes(4)

Return Type :Boolean

Description : Return true if the value passed to includes is in the array

Check if the value 4 is in the array

result : false

4.length

[1,2,3].length

Return Type :Number

Description : Return the length of the array

result : 3