Skip to main content

Command Palette

Search for a command to run...

Array methods continuation...

Published
•3 min read•View as Markdown
Array methods continuation...
M

Hello all,

I am Manasa, I was a Mechanical Engineer for 2 years and embarked on my career in Front-end-web development from 2022. Looking forward to sharing the content that I have grasped through my experience and learnings on this platform.

shift()method

 Shift method helps to remove first element from an array. It modifies the array and also gives new length

image.png

unshift()method

Unshift method helps to add elements to an array from the beginning.It modifies the array and also gives new length

image.png splice()method

Splice()method changes the contents of an array by removing or replacing existing elements and or adding new elements.

In the below example, At index 1 replace the array with word "Feb" without deleting any item by passing parameter "0"

image.png In this example, I have given start index 4 which means I want to replace 4th index with "may" and passed 2 as delete count to remove the 2 elements after 4th index which results in deleting june and july. If we pass 3 as delete count after 4th index all the elements will be deleted.

image.png

slice()method

slice method returns a shallow copy of original array. It does not modify the original array. we can pass our delimiter and limits to the method. First parameter is the start index, meaning at which index the slicing should start and end parameter does not include in the array.

In 1st example: Here only start index is provided which will provide the new array from Orange since end parameter is not given it includes everything in the array.

image.png

In 2nd example: Here we have given two values start index and last index. Last index does not include in our slicing so hence provides only two values. image.png

toString()method

toString() method helps in converting an array to a complete  sentence in a string. It does not modify the original array. 

image.png

join()method

join() method also works same like toString() method but the speciality of Join method is it helps in adding a separator. It does not modify the original array.

image.png

includes()method

  Includes() method determines whether an array includes a certain value among its entries returning true or false. 

includes() method is case sensitive.
Syntax of includes() method is image.png

image.png In this case it is true even though the index position of element 2 is 1. Here -100 is called as formIndex and "array length"+ "formIndex" is called as computed Index. If Computed index is negative then it searches the whole array and returns true.

image.png

filter()method

Filter() method is helpful in checking elements from an array when there are duplicates or when we wanted to run any function.

image.png

sort()method

sort() method sorts the elements in the array from ascending order and firstly it converts all the elements into strings and conversion happens based on UTF-16 code units.

reverse()method

reverse() method returns reference to the original array, so mutating the returned array will mutate the original array as well. So It modifies the original array.

image.png