JavaScript Arrays Cheat Sheet

This article is a JavaScript Arrays Cheat Sheet. Basically, we are going to look at a list of JavaScript 1 liner code snippets for Arrays. It is immensely useful when you’re looking to quickly perform some menial or important tasks where you don’t want to use loops. So, let’s begin. 

Prerequisite

  • Familiar with JavaScript Syntax 
  • Working Knowledge About Arrays 

Basic Methods

A. JavaScript Array concat 

OUTPUT = [1,2,3,4,5] 

Concatenate the array passed to concat to the end of the array Add the elements 4 and 5 to the end of the array 

B. JavaScript Array slice 

OUTPUT = [2,3] 

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 

C. JavaScript Array reverse 

OUTPUT = [3,2,1] 

Reverse the order of the elements in an array 

D. JavaScript Array join 

OUTPUT = ‘1,2,3’ 

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 

Array Manipulation

A. JavaScript Array push  

OUTPUT = [1,2,3,4,5] 

Add one or more elements to the end of the array Add 4 and 5 to the end of the array. Returns length of the array. 

B. JavaScript Array pop 

OUTPUT = [1,2] 

Remove the last element from the array Remove the value 3 from the array 

C. JavaScript Array shift 

OUTPUT = [2,3] 

Remove the first element from the array Remove the value 1 from the array 

D. JavaScript Array unshift 

OUTPUT = [4,5,1,2,3] 

Add one or more elements to the start of the array Add 4 and 5 to the start of the array 

E. JavaScript Array splice 

OUTPUT = [1,4,5,3] 

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. 

F. JavaScript Array forEach 

OUTPUT = 1 2 3 

Execute the function passed to forEach for each element in the array similar to a for loop Log out each element in the array. 

G. JavaScript Array map 

OUTPUT = [2,4,6] 

Return a new array that contains the return result of running each item through the function passed to map Create a new array with all values doubled 

H. JavaScript Array filter 

OUTPUT = [3] 

Return a new array that only contains values that return true to the function passed to filter Create a new array with only values greater than 2 

I. JavaScript Array find 

OUTPUT = [2] 

Select the first element that returns true to the function passed to find Get the first element greater than 1 

J. JavaScript Array findIndex 

OUTPUT = 1 

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 

K. JavaScript Array every 

OUTPUT = false 

Return true if every element in the array returns true from the function passed to every Check if every element is greater than 1 

L. JavaScript Array some 

OUTPUT = true 

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 

M. JavaScript Array reduce 

OUTPUT = 6 

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 

Basic Value Checks

A. JavaScript Array indexOf 

OUTPUT = 0 

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 

B. JavaScript Array lastIndexOf 

OUTPUT = 2 

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 

C. JavaScript Array includes 

OUTPUT = false 

Return true if the value passed to includes is in the array Check if the value 4 is in the array 

D. JavaScript Array length 

OUTPUT = s3 

Return the length of the array 

And that’s a wrap! 

Thank you for going through my article, JavaScript Arrays Cheat Sheet. I hope that you found it useful and were able to comprehend it. Feel free to share your thoughts and opinions, in the comments section down below. Looking forward to an optimistic response! 

Have a great one!