All About JavaScript Array Methods
JavaScript is one of the top most popular programming language for front-end web developers nowadays. For the popularity of javaScript in front-end development, Ryan Dahl develops another server-side runtime NodeJS for backend development. To simplify web development for the developers EcmaScript is adding many beautiful patterns to javaScript. Today I will try to discuss about the methods of the array in javaScript.
array.length()
This is always returns the number of elements in the array.
const peoples = [“John”, “Kerry”, “Ana”];
peoples.length(); // Returns 3
array.push()
Add a new items to the end of the array and returns index of new item.
const peoples = [“John”, “Kerry”, “Ana”];
peoples.push(“Ghana”); // Returns 4
console.log(peoples); // “John”, “Kerry”, “Ana”, “Ghana”
array.pop()
Remove the last item in the array and return the removed item
const peoples = [“John”, “Kerry”, “Ana”];
peoples.pop(); // Returns “Ana”
console.log(peoples); // “John”, “Kerry”
array.shift()
Remove the first item in the array and return the removed item
const peoples = [“John”, “Kerry”, “Ana”];
peoples.shift(); // Returns “John”
console.log(peoples); // “Kerry”, “Ana”
array.unshift()
Add a new items to the start of the array and
const peoples = [“John”, “Kerry”, “Ana”];
peoples.unshift(“Ghana”);
console.log(peoples); // “Ghana”, “John”, “Kerry”, “Ana”
array.reverse()
Reverse the position of all items in the array
const peoples = [“John”, “Kerry”, “Ana”];
peoples.reverse(); // Returns “Kerry”, “Ana”, “John”
array.join(seperator)
Converts the array to a string and all items andseperated with the value of seperator paramitter.
const peoples = [“John”, “Kerry”, “Ana”];
peoples.join(“ “); // Returns “John Kerry Ana”
array.filter()
This return a new array with all elements that pass the test implemented by the provided function.
const numbers = [17, 45, 19, 51, 8, 3, 155];
numbers.filter((number) => number > 50); // Returns [51, 155]
array.find()
This return the first elements that matched the test implemented by the provided function.
const numbers = [17, 45, 19, 51, 8, 3, 155];
numbers.filter((number) => number > 50); // Returns 51
array.forEach()
This return the provided functions return for each items in the array.
const numbers = [2, 5, 3, 10, 15];
numbers.forEach((number) => number * 2); // Returns [4, 10, 6, 20, 30]
Thank you.