Native JavaScript Array Methods - Read Methods
This collection includes non-mutating JavaScript array methods reimplemented from scratch. Explore how methods like
find, at, every, some, includes, and indexOf behave internally — all of which access or test array values without
modifying the original array.
Array.find
The find method returns
the first element in the array that satisfies the provided testing function.
If no match is found, it returns undefined. Ideal for retrieving an item based on a condition without iterating the entire array manually.
Array.prototype.customFind = function (callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i])) {
return this[i];
}
}
};
const array1 = [5, 12, 8, 130, 44];
const found = array1.customFind((element) => element > 10);
console.log(found); // 12
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
const result = inventory.customFind(({ name }) => name === "cherries");
console.log(result); // { name: 'cherries', quantity: 5 }
const result1 = inventory.customFind(({ name }) => name === "nothing");
console.log(result1); // undefinedArray.findIndex
The findIndex()
method returns the index of the first element that passes the provided test function.
If no element matches, it returns -1. Useful for locating the position of an item instead of the item itself.
Array.prototype.customFindIndex = function (callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i])) {
return i;
}
}
return -1;
};
const array1 = [5, 12, 8, 130, 44];
const found = array1.customFindIndex((element) => element > 10);
console.log(found); // 1
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
const result = inventory.customFindIndex(({ name }) => name === "cherries");
console.log(result); // 2
const result1 = inventory.customFindIndex(({ name }) => name === "nothing");
console.log(result1); // -1Array.findLast
The findLast() method
iterates the array in reverse and returns the first value that satisfies the given condition.
If no element matches, undefined is returned. Useful when you want the last matching item in an array without reversing it manually.
Array.prototype.customFindLast = function (callback) {
for (let i = this.length; i >= 0; i--) {
if (callback(this[i])) {
return this[i];
}
}
};
const array1 = [5, 12, 8, 130, 44];
const found = array1.customFindLast((element) => element > 10);
console.log(found); // 44Array.at
The at()
method takes an integer and returns the item at that index — supporting both positive and negative integers.
Negative values count from the end of the array, making it cleaner than using array.length - index.
This example shows a custom implementation of Array.prototype.at() to mimic the native behavior.
Array.prototype.customAt = function (index) {
if (index < 0) {
index = this.length + index;
}
return this[index];
};
const array1 = [5, 12, 8, 130, 44];
console.log(array1.customAt(-1)); // 44Array.every
The every()
method tests whether all elements in an array pass the test implemented by the provided callback.
It returns a boolean — true if all elements satisfy the condition, and false otherwise.
This example includes a custom implementation of Array.prototype.every() and shows how it can be used for subset checks.
Array.prototype.customEvery = function (callback) {
for (let i = 0; i < this.length; i++) {
if (!callback(this[i], i)) {
return false;
}
}
return true;
};
const arr = [1, 30, 39, 29, 10, 13];
console.log(arr.customEvery((currentValue) => currentValue > 40));
// false
console.log(arr.customEvery((currentValue) => currentValue < 40));
// true
const isSubset = (array1, array2) =>
array2.customEvery((element) => array1.includes(element));
console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 7, 6])); // true
console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7])); // falseArray.includes
The includes()
method checks if an array contains a specific value, returning true or false.
It uses the SameValueZero equality comparison — meaning it treats NaN as equal to NaN.
This example includes a custom implementation of Array.prototype.includes() with support for optional fromIndex and negative indexing behavior.
function sameValueZero(x, y) {
return (
x === y ||
(typeof x === "number" && typeof y === "number" && x !== x && y !== y)
);
}
Array.prototype.customIncludes = function (searchElement, fromIndex = 0) {
const length = this.length;
if (length === 0) {
return false;
}
if (fromIndex < 0) {
fromIndex = Math.max(length + fromIndex, 0);
}
for (let i = fromIndex; i < length; i++) {
if (sameValueZero(this[i], searchElement)) {
return true;
}
}
return false;
};
console.log([1, 2, 3].customIncludes(2)); // true
console.log([1, 2, 3].customIncludes(4)); // false
console.log([1, 2, 3].customIncludes(3, 3)); // false
console.log([1, 2, 3].customIncludes(3, -1)); // true
console.log([1, 2, NaN].customIncludes(NaN)); // true
console.log(["1", "2", "3"].customIncludes(3)); // false
const arr = ["a", "b", "c"];
// Since -100 is much less than the array length,
// it starts checking from index 0.
console.log(arr.customIncludes("a", -100)); // true
console.log(arr.customIncludes("a", -2)); // false
console.log(arr.customIncludes("a", -3)); // trueArray.indexOf
The indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
It uses strict equality (===) for comparison and supports an optional fromIndex to control the search starting point.
This custom implementation of Array.prototype.indexOf() also handles negative indexing by adjusting the start position.
Array.prototype.customIndexOf = function (searchElement, fromIndex = 0) {
if (fromIndex < 0) {
fromIndex = this.length + fromIndex;
}
for (let i = fromIndex; i < this.length; i++) {
if (this[i] === searchElement) {
return i;
}
}
return -1;
};
const beasts = ["ant", "bison", "camel", "duck", "bison"];
console.log(beasts.customIndexOf("bison")); // 1
// Start from index 2
console.log(beasts.customIndexOf("bison", 2)); // 4
console.log(beasts.customIndexOf("giraffe")); // -1
const array = [2, 9, 9];
console.log(array.customIndexOf(9, 2)); // 2
console.log(array.customIndexOf(2, -1)); // -1
console.log(array.customIndexOf(2, -3)); // 0
console.log(array.customIndexOf(2, -100)); // 0Array.some
The some()
method tests whether at least one element in the array passes the test implemented by the provided function.
It returns true if any element satisfies the condition; otherwise, it returns false. This method does not modify the original array.
This custom implementation of Array.prototype.some() iterates through the array and returns true if any element passes the provided test.
Array.prototype.customSome = function (callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return true;
}
}
return false;
};
const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.customSome(even)); // true
const equal90 = (element) => element === 90;
console.log(array.customSome(equal90)); // false