10 Most Common Javascript Array Methods
An array is a special variable, which can hold more than one value at a time. Javascript has many useful methods that will help you resolve any task which involves arrays. In this post, I’m going to explain 10 most common array methods every developer should know.
1. join() Method
It returns a new string by concatenating all of the elements in an array separated by a specified separator
const array = ["m","o","n","d","a","y"];
console.log(array.join(""));
// output: monday
2. concat() Method
It returns a new array by merging two or more values/array.
const fruits = ["apple", "strawberry", "orange"];
const vegetables = ["tomato", "zucchini"];const fruits_and_vegetables = fruits.concat(vegetables);
console.log(fruits_and_vegetables);// output:
// [ 'apple', 'strawberry', 'orange', 'tomato', 'zucchini' ]const vegetables_and_fruits = vegetables.concat(fruits);
console.log(vegetables_and_fruits);// output:
// [ 'tomato', 'zucchini', 'apple', 'strawberry', 'orange' ]
3. sort() Method
It sorts the elements of a given array in a specific ascending or descending order.
const months = ["April", "Feb", "Jan", "March", "Dec"];
months.sort();
console.log(months);
// output:
// ["April", "Dec", "Feb", "Jan", "March"]
4. includes() Method
It checks if an array has a certain value among its entries and returns true or false.
const frameworks = ["React", "Vue", "Angular"];
let result1 = frameworks.includes("React");
console.log(result1); // truelet result2 = frameworks.includes("Javascript");
console.log(result2); // false
5. filter() Method
It creates a new array with all elements that pass the test implemented by the provided function.
const days = ["monday", "tuesday", "wednesday", "thursday"];const result = days.filter(day => day.length > 7);console.log(result);
// output:
// ["wednesday", "thursday"]
6. find() Method
It returns a value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
function isEven(element) {
return element % 2 == 0;
}const numbers = [3, 13, 16, 25, 28, 7, 95];
const firstElement = numbers.find(el => isEven(el));console.log(firstElement);
// output: 16
7. forEach() Method
It executes a provided function once for each array element.
const animals = ["cat", "dog", "bird"];animals.forEach(element => console.log(element));// output:
// cat
// dog
// bird
8. push() Method
It adds zero or more elements to end of an array and returns the new length of the array.
const animals = ["cat", "dog", "bird"];let count = animals.push("sheep");
console.log(count);
// output: 4console.log(animals);
// output:["cat", "dog", "bird", "sheep"]
9. pop() Method
It removes the last element from an array and returns that element.
const languages = ["JavaScript", "Python", "Java"];
const removed = languages.pop();
console.log(languages); // [ "JavaScript", "Python" ]
console.log(removed); // Java
10. map() Method
It creates a new array with the results of calling a function for every array element
const numbers = [2, 4, 6, 8];
let newNumbers = numbers.map(x => x * 2);console.log(newNumbers);
// output: Array [4, 8, 12, 16]
Conclusion
I listed down most common methods of array. Using these methods with little bit of creativity, you can already pass some of the most popular whiteboard interview questions you will face in the future. Good luck! Happy coding
Thanks for reading