Skip to content
Menu
Rohit Naik Kundaikar
  • Home
  • Contact
Rohit Naik Kundaikar

JS Arrays Cheatsheet

Posted on June 2, 2021 by Rohit Naik Kundaikar

push(value)

Simply adds new value at the end of the array.

var numbers = [1,2,3,4];
numbers.pop(5)
// Output : 1,2,3,4,5

unshift(value)

Like push unshift also adds elements to the array, but at the start of the array instead the end of array.

var numbers = [1,2,3,4];
numbers.unshift(5)
// Output : 5,1,2,3,4

pop()

Removes the last element from the array.

var numbers = [1,2,3,4,5];
numbers.pop()
// Output : 1,2,3,4

shift()

It would simply remove the first element in the array, unlike pop() which removes the last element in the array.

var numbers = [1,2,3,4,5];
numbers.shift()
// Output : 2,3,4,5

slice(i, j+1)

Here i is the start index of the splice and j+1 is the end index of the splice. Note that we take the position as the second argument and not the end index. The position is simply index +1

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.slice(2,5)

// Output: 3,4,5

splice(i,j,[])

You can use the Splice function to remove or add elements in an array. It also modifies original array.

Here i refers to the start index, j refers to a number of elements that needs to be removed from i index.

var fruits = ["Orange", "Apple", "Mango", "Banana"];  
fruits.splice(2, 1, ["Kiwi", "Lemon"]);

// Output: Orange,Apple,Kiwi,Lemon,Banana
// Note: here Kiwi and Lemon are added to array while Mango has been removed since second argument is 1
// so it will remove 1 element from index 2

reverse()

It simply reverses all the elements in an array. It also modifies original array.

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.reverse();

reduce(total, value) or reduceRight(total, value)

reduce function is used when you want to calculate something while iterating over the array. reduce() has two arguments i.e total and current value. Note total is assigned the first index of the array while value starts from the second value in an array.

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.reduce((total,num)=>total-num);
// Output : -53
numbers.reduceRight((total,num)=>total+num);
// Output : -35

find()

It return the first value for which the return was true.

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.find((x)=> x==5)
// Output : 5 
// if value is not present the it simply returns undefined

findIndex() and lastIndexOf()

It return the first index for which the return was true.

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.findIndex((x)=> x==5)
// Output : 4 
// if value is not present the it simply returns -1

filter()

filter simply returns all the elements for which the return is true.

var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers.filter((num)=> num > 5);
// Output : 6,7,8,9,10

Uncategorized

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Typescript Guide – Part 1
  • Vite Micro Frontend
  • React Best Practice: Part 2
  • React Best Practices: Part 1
  • Redux Toolkit

Recent Comments

    Archives

    • August 2024
    • January 2024
    • September 2021
    • July 2021
    • June 2021
    • May 2021
    • April 2021
    • December 2020
    • November 2020
    • October 2020
    • September 2020

    Categories

    • Angular
    • API
    • Best Practice
    • Compiler
    • Context
    • DevOps
    • Docker
    • FAANG
    • Forms
    • GraphQL
    • Java
    • Javascript
    • Machine Learning
    • MobX
    • Python
    • ReactJS
    • Redux Toolkit
    • Spring Boot
    • Typescript
    • Uncategorized
    • Vite
    • Webpack
    ©2025 Rohit Naik Kundaikar | Powered by WordPress & Superb Themes