Cityscape
Girl

JavaScript

Working with Arrays

Easy
15 min

More array operations!

We can do a lot more with arrays than push or pop items from them.

Checking if an Array Contains a Value

JavaScript provides a simple way to check if an array includes a certain value using the includes() method. When you call array.includes(value), it returns true if the value is found and false otherwise.

let fruits = ["apple", "banana", "cherry"];
console.log(fruits.includes("banana"));  // prints true
console.log(fruits.includes("mango"));   // prints false

Finding the Index of an Element

To find the position of a value in an array, you can use the indexOf() method. This method returns the index of the first occurrence of the specified element, or -1 if the element is not found.

let fruits = ["apple", "banana", "cherry"];
console.log(fruits.indexOf("banana"));  // prints 1
console.log(fruits.indexOf("mango"));   // prints -1

Searching Arrays with find()

What Is Array.find()?

The Array.find() method helps you search through an array to locate the first element that meets a certain condition. When you call array.find(callback), JavaScript goes through the array and calls the callback function on each element. When the callback returns true, find() returns that element. If no element satisfies the condition, it returns undefined.

Using a Named Function

Sometimes, it's helpful to write the callback function separately (as a named function) before passing it to find(). This makes your code easier to understand, especially when the condition is complex.

For example, imagine you have an array of numbers and you want to find the first even number. You might write:

function isEven(num) {
  return num % 2 === 0;
}

let numbers = [1, 3, 5, 8, 10];
let firstEven = numbers.find(isEven);
console.log(firstEven);  // prints 8

Removing an Element by Index with splice()

Sometimes, you want to remove an element at a specific position in an array. The splice() method allows you to remove elements at a given index. When you call splice(index, count), it removes the specified number of elements starting at the given index and returns an array of removed items.

For example:

let numbers = [10, 20, 30, 40];
let removed = numbers.splice(2, 1);
console.log(numbers);  // prints [10, 20, 40]
console.log(removed);  // prints [30]

Removing Elements by Condition with filter()

The filter() method creates a new array that includes only the elements that satisfy a certain condition. Unlike splice(), filter() does not change the original array—it returns a new array.

For example, if you want to remove all occurrences of the value 5 from an array:

let values = [5, 10, 5, 20, 5, 30];
let filtered = values.filter(function(item) {
  return item !== 5;
});
console.log(filtered);  // prints [10, 20, 30]

Exercises

Check for a Value with includes()

  • Create a variable named fruits using let and assign it an array containing "apple", "banana", and "cherry".
  • Use the includes() method to check if "banana" is in the array, and print the result.
  • Also, check if "mango" is in the array, and print the result.

Find the Index of an Element with indexOf()

  • Create a variable named fruits using let and assign it an array containing "apple", "banana", and "cherry".
  • Use the indexOf() method to find the index of "cherry" in the array.
  • Print the index using console.log.

Find with a Named Function

  • Create an array called numbers using let that contains the values [7, 11, 13, 16, 19].
  • Write a named function called isEven that returns true if a number is even.
  • Use numbers.find() with the named function to get the first even number and store it into a variable called firstEven.
  • Print the result using console.log.

Use an Arrow Function Inline with Array.find()

  • Create an array called numbers using let with the values [2, 5, 8, 11].
  • Use the arrow function syntax inline with the find() method to locate the first number greater than 7.
  • Print the found value using console.log.

Remove an Element by Index with splice()

  • Create an array named numbers and assign it the values [10, 20, 30, 40].
  • Use the splice() method to remove the element at index 2.
  • Store the removed element in a variable called removed.
  • Print the updated numbers array and the removed element using console.log

Remove Elements by Condition with filter()

  • Create an array named values and assign it the values [5, 10, 5, 20, 5, 30].
  • Use the filter() method to create a new array called filtered that excludes all occurrences of the value 5.
  • Print the new array filtered using console.log.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.