Find, test, and sort arrays
ECMAScript 2026
Use a callback to decide which item qualifies or how two items should be ordered.
On this page
find() returns the first match
find() passes each item to a callback and returns the first one that passes. If none passes, it returns undefined. Use optional chaining before reading properties from a possibly missing match.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const catalog = [
{ name: "Pen", price: 1500 },
{ name: "Notebook", price: 2500 },
{ name: "Folder", price: 3000 }
];
const match = catalog.find(item => item.price > 2000);
console.log(match?.name);
What to expect Notebook matches first. Change the threshold to 4000 to see undefined.
some() tests whether at least one item passes
some() returns true as soon as an item passes the callback’s test. With zero matching items, it returns false.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const catalog = [
{ name: "Pen", price: 1500 },
{ name: "Notebook", price: 2500 },
{ name: "Folder", price: 3000 }
];
console.log(catalog.some(item => item.price < 2000));
console.log(catalog.some(item => item.price < 1000));
What to expect True for a price below 2000, false for a price below 1000.
every() tests whether all items pass
every() returns false the first time something fails. It returns true if no item fails, including for an empty array.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const catalog = [
{ name: "Pen", price: 1500 },
{ name: "Notebook", price: 2500 },
{ name: "Folder", price: 3000 }
];
console.log(catalog.every(item => item.price > 0));
console.log(catalog.every(item => item.price < 2000));
What to expect All prices are positive, but at least one is 2000 or above.
sort() orders items in place
Without a comparator, sort() compares string representations. For numbers, a - b sorts low to high. sort() edits the array itself. Use toSorted() to produce a sorted copy.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const prices = [10, 2, 30];
console.log([...prices].sort((a, b) => a - b));
console.log(prices);
prices.sort((a, b) => a - b);
console.log(prices);
What to expect The sorted copy is [2, 10, 30]. The original stays [10, 2, 30] until sort() runs.