Menu
Coddy logo textTech

Array Methods Part 2

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 61 of 77.

Arrays have more built-in methods that can help us with common tasks. Here are some of them:

  • sort() - sorts the elements of an array.
  • reverse() - reverses the order of elements in an array.
  • indexOf(element) - returns the index of the first occurrence of an element in the array, or -1 if it's not found.
  • lastIndexOf(element) - returns the index of the last occurrence of an element in the array, or -1 if it's not found.
  • includes(element) - returns true if the array contains the specified element, otherwise returns false.

Here is an example of how to use the sort method:

let myArray = ["orange", "banana", "apple"];
myArray.sort();
console.log(myArray);

This will output ["apple", "banana", "orange"].

Example of the reverse method:

let myArray = [1, 2, 3, 4, 5];
myArray.reverse();
console.log(myArray);

This will output [5, 4, 3, 2, 1].

Example of the indexOf method:

let myArray = [10, 20, 30, 40, 50, 30];
let index = myArray.indexOf(30);
console.log(index);

This will output 2.

Example of the lastIndexOf method:

let myArray = [10, 20, 30, 40, 50, 30];
let index = myArray.lastIndexOf(30);
console.log(index);

This will output 5.

Example of the includes method:

let myArray = ["apple", "banana", "orange"];
let hasBanana = myArray.includes("banana");
console.log(hasBanana);

This will output true.

challenge icon

Challenge

Easy

Create a function named findElement that receives two arguments:

  • An array of numbers
  • A number to search for

The function should do the following:

  1. Check if the array includes the number using the includes method.
  2. If the number is found, return the index of its first occurrence using the indexOf method.
  3. If the number is not found, return -1.

For example:

  • findElement([1, 2, 3, 4, 5], 3) should return 2
  • findElement([1, 2, 3, 4, 5], 6) should return -1

Cheat sheet

JavaScript arrays have several built-in methods for common operations:

  • sort() - sorts the elements of an array
  • reverse() - reverses the order of elements in an array
  • indexOf(element) - returns the index of the first occurrence of an element, or -1 if not found
  • lastIndexOf(element) - returns the index of the last occurrence of an element, or -1 if not found
  • includes(element) - returns true if the array contains the element, otherwise false

Examples:

let fruits = ["orange", "banana", "apple"];
fruits.sort(); // ["apple", "banana", "orange"]

let numbers = [1, 2, 3, 4, 5];
numbers.reverse(); // [5, 4, 3, 2, 1]

let arr = [10, 20, 30, 40, 50, 30];
arr.indexOf(30); // 2 (first occurrence)
arr.lastIndexOf(30); // 5 (last occurrence)
arr.includes(30); // true

Try it yourself

function findElement(arr, num) {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals