JavaScript Array Method Part-1

Genius
4 min readMay 5, 2021

This is the first part of js array method manipulation. Here I am trying to explain some array methods. Hope you will get some knowledge on the array method.

concat()

array.concat() method is used to merge two or more arrays and converting a single array without changing the existing arrays. Existing arrays remain the same and return a new array.

Here fruits4 is created from merging between fruits1 & fruits2 array. There is no change happen in the existing array.

join()

The join() method converts an array into a string. The elements will be separated by a specified separator. The default separator is a comma (,). This method doesn’t change the existing array.

some() & every()

Array. some() method checks whether at least one element of the array can pass the given particular test or not. It returns true or false.

Array.every() method checks whether all the elements of the array can pass the given particular test or not. It returns true or false.

Here in the example,

First function is numGreaterThan8().This function checks that elements of the array are greater than 8 or not. For some() method it returns false because not a single element of the numberArray isn’t greater than 8. Similarly, for every() method all the elements of numberArray aren’t greater than 8.

The second function is numLessThan8. Here for some() method at least one element is less than 6. So it returns true. every() method also returns true because all the elements are less than 6.

The third function is numLessThan6. Here for some() method we see at least one element is less than 6 but not all the element is less than 6. That’s why here some() returns true whether every() returns false.

pop(),push() & shift() ,unshift()

pop(): remove an item at the end of the array and returns the item

push(): add an item at the end of the array and return the length of the array

shift(): Remove an item from the beginning of an array and returns the removed item.

unshift(): Add items to the beginning of an array and returns the new array length.

Let’s numbers is an array. pop() removes an element end of an array and returns it. Here the last element is 15. So it returns under the variable popNumber. push() method doing the opposite of pop(). It adds an item end of the array. Here by doing push() method 15 is added end of the array.

shift() removes an element beginning of the array. Here the first element is 11 and it removes after applying the shift() method. unshift() doing exact the opposite of shift().

sort() & reverse()

sort(): The sort() method sorts the elements of an array and returns the sorted array. The default sort order is ascending.

reverse(): The reverse() method just reverses the array . Here the first element becomes the last and vice-versa.

map() & forEach()

The map() method creates a new array with the results of calling a function for every array element.The forEach() method calls a function once for each element in an array, in order. The map() method returns an entirely new array with transformed elements and the same amount of data. In the case of forEach(), even if it returns undefined

--

--

Genius
0 Followers

Curious Learner of JS and its framework