JavaScript Array Methods — Part 1: Finding & Checking
What is an Array?
An array is a sequential collection of data. Think of it as a long box divided into compartments — you can put apples in one compartment, oranges in another. In JavaScript, those compartments can hold anything: numbers, strings, booleans, even other arrays.
const arra = [1, 'John', true];
console.log(arra[0], arra[1], arra[2]); // Output: 1 'John' true
One important thing — array elements are read from 0, not 1. So arra[0] gives you the first element, arra[1] gives the second, and so on.
What are Array Methods?
Array methods are functions that come built-in automatically with every array we define. We don't need to write them ourselves — they're always there, ready to use. We can do specific operations with them.
For all examples below, I'll use this array:
const arr = [5000, 500, 3400, -150, 790, -3210, -1000, 500, -30];
find
Checks all elements and returns the first one that matches the condition.
const find = arr.find(element => element === 500);
console.log(find); // Output: 500
Notice — 500 appears twice in our array (2nd and 8th position). find returned the first one, not the last.
findIndex
Same as find but instead of returning the element, it returns its index (placement number in the array).
const findIndex = arr.findIndex(element => element === 500);
console.log(findIndex); // Output: 1
Logically you might expect 2 since it's the second element — but remember, arrays start from 0, so the second element is at index 1.
findLast
Opposite of find. Returns the last element that matches the condition instead of the first.
const findLast = arr.findLast(element => element === 500);
console.log(findLast); // Output: 500
findLastIndex
Opposite of findIndex. Returns the index of the last matching element.
const findLastIndex = arr.findLastIndex(element => element === 500);
console.log(findLastIndex); // Output: 7
some
Returns true if at least one element matches the condition. Otherwise false.
const some = arr.some(element => element > 0);
console.log(some); // Output: true
Our array has positive numbers, so it returns true.
every
Returns true only if all elements match the condition. Otherwise false.
const every = arr.every(element => element > 0);
console.log(every); // Output: false
Our array has negative numbers too, so not every element is greater than 0 — returns false.
includes
Checks if a specific value exists in the array. Returns true or false.
const includes = arr.includes(-30);
console.log(includes); // Output: true
Quick Comparison
| Method | What it checks |
|---|---|
some |
If any element fulfills the condition |
every |
If all elements fulfill the condition |
includes |
If the array contains a specific value |
Part 2 coming soon — forEach, map, filter, and reduce.
