An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
7 Essential Array Methods In JavaScript (2021)
Looking for the easiest way to add, remove or find elements from an array? After reading this article you'll be able to do basic JS operations related to an array in just one line. Each method is explained with a code example.
Overall there are several methods available in the JavaScript ecosystem. This article will cover the basic 7 methods through which you will be able to add, remove or find elements from an array in no time.
Let's get started by assuming five vegetable emojis in an array.
PUSH
The push method adds an element to the last position of an array.
The push() can be used with apply() to merge arrays.
UNSHIFT
The unshift method adds an element to the first position of an array.
The unshift() can be used to add a nested array at the beginning of an array.
POP
The pop method removes the last element from an array.
Using pop() method on an empty array will returns undefined
CONCAT
The concat method can merge multiple arrays and returns a single combined array.
The conact() can be used to merge any type of object in an array.
SHIFT
The shift method removes the first element from an array.
The shift() can be used to iterate through the array while printing the results.
INDEXOF
The indexOf method returns the first position of an element which can be found in an array.
This IndexOf() can be used to find occurrences of an element in an array.
LASTINDEXOF
The lastIndexOf method returns the last position of an element which can be found in an array.
This lastIndexOf() can be used to find occurrences of an element in an array.
Now, you'll be able to perform basic operations using array methods.
Sign up for the Medium newsletter to get notified about the next blog JavaScript posts and possible future giveaways
Happy coding!
Original Link: https://dev.to/jatinkrr/7-essential-array-methods-in-javascript-you-need-2021-m73
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To