JavaScript Program for Linear Search with Example
A JavaScript program for linear search checks each element in an array one by one using a
for loop and returns the index if the element matches, like function linearSearch(arr, target) { for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } return -1; }.Examples
Input[3, 5, 7, 9], target=7
Output2
Input[10, 20, 30, 40], target=25
Output-1
Input[], target=1
Output-1
How to Think About It
To find a target value in a list, start from the first item and check each one in order. If you find the target, stop and return its position. If you reach the end without finding it, return -1 to show it's not there.
Algorithm
1
Start from the first element of the array.2
Compare the current element with the target value.3
If they match, return the current index.4
If not, move to the next element.5
Repeat until the end of the array.6
If the target is not found, return -1.Code
javascript
function linearSearch(arr, target) { for (let i = 0; i < arr.length; i++) { if (arr[i] === target) { return i; } } return -1; } console.log(linearSearch([3, 5, 7, 9], 7)); // 2 console.log(linearSearch([10, 20, 30, 40], 25)); // -1 console.log(linearSearch([], 1)); // -1
Output
2
-1
-1
Dry Run
Let's trace searching for 7 in [3, 5, 7, 9] through the code
1
Check first element
Compare arr[0] = 3 with target 7 → not equal
2
Check second element
Compare arr[1] = 5 with target 7 → not equal
3
Check third element
Compare arr[2] = 7 with target 7 → equal, return index 2
| Index | Array Value | Comparison Result |
|---|---|---|
| 0 | 3 | No |
| 1 | 5 | No |
| 2 | 7 | Yes - Found |
Why This Works
Step 1: Loop through array
The for loop goes through each element one by one to check all possible matches.
Step 2: Compare elements
Each element is compared with the target using === to find an exact match.
Step 3: Return index or -1
If a match is found, the function returns the index immediately; otherwise, it returns -1 after checking all elements.
Alternative Approaches
Using Array.prototype.indexOf
javascript
function linearSearch(arr, target) { return arr.indexOf(target); } console.log(linearSearch([3, 5, 7, 9], 7)); // 2
This is simpler and uses built-in method but less flexible for custom comparisons.
Using while loop
javascript
function linearSearch(arr, target) { let i = 0; while (i < arr.length) { if (arr[i] === target) return i; i++; } return -1; } console.log(linearSearch([3, 5, 7, 9], 7)); // 2
Uses a while loop instead of for loop; functionally same but different style.
Complexity: O(n) time, O(1) space
Time Complexity
The algorithm checks each element once, so time grows linearly with array size.
Space Complexity
Only a few variables are used, so extra space stays constant regardless of input size.
Which Approach is Fastest?
Using indexOf is fastest for simple equality checks, but custom loops allow more flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop linear search | O(n) | O(1) | Custom conditions and clarity |
| Array.indexOf | O(n) | O(1) | Simple equality search |
| While loop linear search | O(n) | O(1) | Alternative loop style |
Use
indexOf for simple searches, but write your own loop for more control.Forgetting to return
-1 when the target is not found causes undefined results.