Popular array handling methods (Array) in JavaScript (Part 2)

Tram Ho

Hi guys, in the previous article I have introduced you to 10 useful methods when working with arrays, today we will continue to elaborate on the great methods of it, let’s begin.

11. push ()

push() helps us add one or more elements at the end of the array

  • push() YES changes the original array
  • push() returns the NEW LENGTH of the array (arr.lenght) after adding the element

Syntax

12. unshift ()

This method is the opposite of push()

unshift() helps us add one or more elements to the beginning of the array

  • unshift() YES changes the original array
  • unshift() returns the new LENGTH of the array (arr.lenght) after adding the element

Syntax

13. reduce ()

This is a method that some newbie just learned anything twisted

reduce() used to execute a function on each element of the array (from left to right) with a cumulative variable to get a unique value.

  • reduce() NOT change the original array
  • reduce() returns the value after minification

Syntax

14. reverse ()

reverse() used to reverse the array, the first element to the last and vice versa

  • reverse() YES changes the original array
  • reverse() returns the array after inversion

Syntax

15. some ()

some() checks to see if at least one element of the array satisfies the condition of the passed function

  • some() DO NOT change the original array
  • some() returns the Boolean type: true if there is at least one element satisfying and false if not satisfied
  • some() will return false if the array is empty

Syntax

15. sort ()

sort() will sort the elements in an array, the elements can be sorted alphabetically or numerically in ascending or descending order.

By default the elements will be sorted alphabetically in ascending order. This makes sort() will sort the string very accurately, but when sorting the numbers will not be correct (for example 20 and 100, 20 will be greater than 100 because 2> 1) we can fix this. by passing a parameter is a comparison array

  • sort() YES changed the original array
  • sort() returns the array after it has been sorted

Syntax

Conclusion

It’s quite a long time, I paused part 2 here.

If you find a good article, give me +1 upvote. If you like me, click the follow button for more cool stuff. Good luck !

See you in part 3!

Share the news now

Source : Viblo