HAKATEMIA
17JavaScript

Working with Arrays

Easy15MIN

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.

JAVASCRIPT
1let fruits = ["apple", "banana", "cherry"];
2console.log(fruits.includes("banana"));  // prints true
3console.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.

JAVASCRIPT
1let fruits = ["apple", "banana", "cherry"];
2console.log(fruits.indexOf("banana"));  // prints 1
3console.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:

JAVASCRIPT
1function isEven(num) {
2  return num % 2 === 0;
3}
4
5let numbers = [1, 3, 5, 8, 10];
6let firstEven = numbers.find(isEven);
7console.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:

JAVASCRIPT
1let numbers = [10, 20, 30, 40];
2let removed = numbers.splice(2, 1);
3console.log(numbers);  // prints [10, 20, 40]
4console.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:

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

Exercises

1 / 2
Hakatemia Pro

Learn to hack — start here

Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.