DEV Community

Cover image for JavaScript Array methods
Ishaan Sheikh
Ishaan Sheikh

Posted on • Updated on

JavaScript Array methods

In this post, we will learn some of the arrays methods available in JavaScript.

1. Array.sort()

This method sorts the items of the array. It takes an optional compareFunction in which you can define your own criteria for sorting.

Syntax

array.sort(compareFunction)
Enter fullscreen mode Exit fullscreen mode

Example

var numbers = [3,6,1,0,9];
console.log(numbers.sort()); // [0,1,3,6,9]
Enter fullscreen mode Exit fullscreen mode

Using compareFunction to sort in descending order.

var numbers = [3,6,1,0,9];
console.log(numbers.sort((a, b) => (a > b ? -1 : 1))); // [9,6,3,1,0]
Enter fullscreen mode Exit fullscreen mode

2. Array.slice()

This method used to get the array of elements from a given position to a given position in an array. Both parameters are optional in this function.

Syntax

array.slice(start, end)
Enter fullscreen mode Exit fullscreen mode

Example

var numbers = [1,2,3,4,5];
console.log(numbers.slice(2,4)); // [3,4]

console.log(numbers.slice(3)); // [4,5]
Enter fullscreen mode Exit fullscreen mode

3. Array.filter()

The filter() method in javascript is used to filter the elements present in the array based on the condition. It takes a mandatory function as an argument in which we define our filtering criteria.

Syntax

array.filter(function(element) { return condition: })
Enter fullscreen mode Exit fullscreen mode

Example

var numbers = [1,2,3,4,5];

console.log(numbers.filter((element) => element % 2 == 0)); //[2,4]
Enter fullscreen mode Exit fullscreen mode

In the above example, the filter function returns only even numbers.

4. Array.reverse()

The reverse() function in javascript reverses the order of elements in an array.

Syntax

array.reverse()
Enter fullscreen mode Exit fullscreen mode

Example

var cars = ["Audi", "Ford", "BMW"];
console.log(cars.reverse()); // ["BMW", "Ford", "Audi"]
Enter fullscreen mode Exit fullscreen mode

5. Array.includes()

This method is used to check whether the array contains an element or not. It returns true if the element exists in the array and false otherwise.

Syntax

array.includes(element, start);
Enter fullscreen mode Exit fullscreen mode

The start is an optional argument, it defines at which index should the searching should starts.

Example

var cars = ["Audi", "Ford", "BMW"];

console.log(cars.includes("Tesla")); // false
Enter fullscreen mode Exit fullscreen mode

6. Array.concat()

This method is used to join two or more arrays into a single array.

Syntax

array1.concat(array2, array3,....arrayN);
Enter fullscreen mode Exit fullscreen mode

Example

var cars = ["Audi", "Tesla", "BMW"];
var numbers = [1,2,3];

console.log(cars.concat(numbers)); // ["Audi", "Tesla", "BMW", 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Bonus

Array.prototype constructor

It allows us to add new properties and methods to the Array Object.

Syntax

Array.prototype.yourProperty = value;

Array.prototype.yourMethod = function () {
    console.log(this); // this refers to the array
};
Enter fullscreen mode Exit fullscreen mode

In this post, I showed you some of the array methods. If you want to learn about more javascript array functions consider watching this series of videos by Florin Pop. In this series, he has explained about 24 javascript array methods.

Top comments (2)

Collapse
 
mellen profile image
Matt Ellen • Edited

It might be worth mentioning that sort without a comparison function sorts by converting the elements to strings and using string comparison, so [3,7,2,1,35,24].sort() will end up as [1,2,24,3,35,7].

Collapse
 
frikishaan profile image
Ishaan Sheikh

Thanks for pointing out.