DEV Community

cisneros-a
cisneros-a

Posted on

Different Array Methods (pt. 3)

Array.sort()

Alt Text

What it does:

The sort method will sort an array of integers or strings.

What it returns:

Something to keep in mind is that if you just call the sort method alone, it will not return what you expect. You can see in this example that 10 is returned before 2. This is because it will only compare the first digit in each number.

Array.sort(with a callback)

Alt Text

What it does:

The sort method will sort an array of integers or strings.

What it returns:

This is returning what would you probably expect from a sorting method. Adding the call back will ensure that the method does a deeper check than just the first digit of each number.

Array.flat()

Alt Text

What it does:

This method flattens all the arrays within the array you are calling the method on.

What it returns:

This returns a single array of all the indexes in the array.

Top comments (0)