find
Returns the first matching item or undefined.
Search and sort methods help you find list items, test conditions and order data before rendering it.
const records = [
{ title: "CSS", score: 82 },
{ title: "HTML", score: 94 }
];
const best = records.find((record) => record.score > 90);
Arrays & Collections
Use find when you need the first matching item. Use findIndex when you need its position. Use some or every when you only need a boolean answer.
Sorting needs a comparator function for reliable numeric or custom ordering. Without a comparator, sort converts values to strings.
sort mutates the original array. If you need a sorted copy, use toSorted where available or copy first with spread.
Returns the first matching item or undefined.
Returns the index of the first match or -1.
Boolean checks for any or all items.
Orders the array and mutates it unless using a copy.
Examples
const scores = [10, 2, 30]; const sortedScores = [...scores].sort((a, b) => a - b);
const scores = [10, 2, 30]; scores.sort(); console.log(scores); // [10, 2, 30] becomes string-sorted
Code patterns
These are the patterns developers search for constantly: reading items, copying arrays, choosing map/filter/reduce, sorting with a comparator, destructuring and using Set or Map.
Find the first record that matches a condition.
const records = [{ id: 1 }, { id: 2 }];
const record = records.find((item) => item.id === 2);
Ask whether at least one item matches.
const scores = [72, 88, 91]; const hasHighScore = scores.some((score) => score >= 90);
Always provide a comparator for numbers.
const scores = [10, 2, 30]; scores.sort((a, b) => a - b);
Copy before sorting if original order matters.
const scores = [10, 2, 30]; const sorted = [...scores].sort((a, b) => a - b);
Rules that matter
Arrays and collections are not just storage. They describe whether the code is selecting, transforming, grouping, counting, sorting or enforcing uniqueness.
It returns the item itself, not an array.
Do not use find when more than one match matters.
They communicate the question clearly.
Default sort is string-based.
sort mutates the array.
find can return undefined and findIndex can return -1.
Production thinking
Search and sort code becomes readable when the method name matches the intent.
Search and sort often control visible list order. Users need stable, predictable results and labels.
Production sorting should handle missing values, mixed casing and locale rules deliberately.
Sorted content lists should be deterministic so crawlers and users see stable order.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A collection method should tell the reader what kind of operation is happening before they read the callback body.