Linear Search explained step-by-step: sequential scan mechanics, unsorted array applicability, best/worst/average complexity trade-offs, and JavaScript code.
Linear Search (also called Sequential Search) is the simplest and most direct searching technique. It starts at the first element of a collection and checks each subsequent item one by one until finding a match or exhausting all elements.
Unlike Binary Search, Linear Search places zero preconditions on the input data: it works seamlessly on unsorted arrays, singly linked lists, and streaming data. While its O(n) worst-case time is unsuited for massive static datasets, it is optimal for one-off search queries on small or unsorted lists where the O(n log n) cost of presorting outweighs the query time.
Begin inspecting elements from the first element of the array.
Evaluate whether arr[i] === target. If they match, immediately return the current index i (best-case O(1) if located at index 0).
If the current element does not match, increment index i by 1 and repeat the comparison for the next element.
If the loop completes without finding any matching value, return -1 to signal that the target is absent from the array.
1function linearSearch(arr, target) {2 for (let i = 0; i < arr.length; i++) {3 if (arr[i] === target) {4 return i; // Target found at index i (Best case O(1), Worst case O(n))5 }6 }7 return -1; // Target not present in array8}9 10// Example usage:11// linearSearch([10, 50, 30, 70, 80, 20], 30); // 212// linearSearch([10, 50, 30, 70, 80, 20], 99); // -1Play through every comparison, swap, and state change, adjust the speed, or enter custom inputs to test edge cases.
The full interactive roadmap is unlocking — these specialized modules land in upcoming releases.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
Explore the full catalog — 30+ algorithms with more unlocking every release.
Linear Search ready — scanning elements sequentially for target 42.
Comparisons
0
Scanned Items
0
Target
42
Array Size
10
Test your understanding of Linear Search complexity and step mechanics.
Q1.In what scenario is Linear Search preferable over Binary Search?
Q2.What is the best-case time complexity of Linear Search?
Apply Linear Search to real coding interview questions.
Find all matching indices after scanning array values.