Scan an array from the first element to the last until the target turns up — works on any list, sorted or not.
Linear search is the most basic way to find something: start at the first element and check each one in turn until you hit the target or run off the end. It needs *no setup* and makes *no assumptions* — the list can be sorted, shuffled, or full of duplicates and it still works. That generality is its whole appeal, and the price you pay is speed.
0 — the first element.-1.function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i; // found
}
return -1; // ran off the end
}n/2 elements on average — and all n in the worst case (target last, or absent). Both are O(n): the work grows in lockstep with the list's size.It sounds primitive, but linear search wins in several real cases. On an unsorted list it's the *only* option without sorting first. On a tiny list the simplicity beats the overhead of anything cleverer. And it accesses memory sequentially, front to back, which is exactly the pattern CPU caches are built for — so a linear scan over a small array can outrun a binary search's jumping around.