Linear Search is one of the simplest and most beginner-friendly searching algorithms in programming. The idea is straightforward: start at the beginning of an array and check each element one by one until you find the target value. If the target is found, you return its position; if not, the search continues until the end of the array.
with hands-on learning.
get the skills and confidence to land your next move.
Learning Linear Search is important for beginners because it introduces basic algorithmic thinking, such as iteration, condition checking, and handling arrays. Although it is not the fastest searching algorithm for large datasets, it is highly versatile and works on any type of array, sorted or unsorted. Linear Search is used in applications like checking if a user’s input exists in a list, simple database lookups, and validating items in small datasets.
Program 1: Basic Linear Search using a for loop
This program demonstrates a simple Linear Search implementation using a for loop to search for a target value in an array.
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}
let numbers = [10, 20, 30, 40, 50];
let target = 30;
let result = linearSearch(numbers, target);
console.log(result !== -1 ? `Element found at index ${result}` : "Element not found");This program works by checking each element sequentially. Beginners can understand it as “look at each number one by one until you find the one you want.” It is useful because it works on any array without needing sorting.
Program 2: Linear Search using while loop
This version uses a while loop instead of a for loop to perform the search.
function linearSearchWhile(arr, target) {
let i = 0;
while (i < arr.length) {
if (arr[i] === target) {
return i;
}
i++;
}
return -1;
}
let values = [5, 8, 12, 15, 20];
let searchValue = 12;
let resultWhile = linearSearchWhile(values, searchValue);
console.log(resultWhile !== -1 ? `Element found at index ${resultWhile}` : "Element not found");Using a while loop demonstrates an alternative way to iterate through an array. Beginners can see that both for and while loops can achieve the same goal, giving flexibility in coding. It is useful when the number of iterations is not fixed in advance.
Program 3: Linear Search using recursion
This program demonstrates how Linear Search can be implemented recursively, giving a different perspective on solving problems.
function linearSearchRecursive(arr, target, index = 0) {
if (index >= arr.length) return -1;
if (arr[index] === target) return index;
return linearSearchRecursive(arr, target, index + 1);
}
let data = [3, 6, 9, 12, 15];
let searchTarget = 12;
let resultRecursive = linearSearchRecursive(data, searchTarget);
console.log(resultRecursive !== -1 ? `Element found at index ${resultRecursive}` : "Element not found");This program works by checking one element per recursive call. Beginners can understand it as “keep checking the next element until you reach the end.” Recursion helps learners think about problem-solving in terms of breaking down tasks into smaller, repeatable steps.
Program 4: Linear Search with multiple occurrences
This example shows how to modify Linear Search to find all occurrences of a target value in an array.
function linearSearchAll(arr, target) {
let indices = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
indices.push(i);
}
}
return indices;
}
let numbersMultiple = [4, 7, 4, 2, 4, 9];
let targetMultiple = 4;
let allResults = linearSearchAll(numbersMultiple, targetMultiple);
console.log(allResults.length ? `Element found at indices: ${allResults}` : "Element not found");This program works by storing all indices where the target is found. Beginners can understand it as “keep track of every place the number appears.” It is useful when duplicates exist in the array and all positions of the element are needed.
Program 5: Linear Search as a reusable function
This program demonstrates a clean and reusable Linear Search function suitable for multiple projects.
function linearSearchUtility(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
let dataset = [11, 22, 33, 44, 55];
let findValue = 33;
let resultUtility = linearSearchUtility(dataset, findValue);
console.log(resultUtility !== -1 ? `Element found at index ${resultUtility}` : "Element not found");This utility function can be used anywhere in your project to search arrays efficiently. Beginners will appreciate that Linear Search can be wrapped neatly into a reusable piece of code, reinforcing good programming practices.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Linear Search in JavaScript in simple terms.
Q1. What is Linear Search in JavaScript?
Linear Search is a method to find an element in an array by checking each element one by one.
Q2. Can Linear Search be used on unsorted arrays?
Yes, Linear Search works on both sorted and unsorted arrays.
Q3. What is the time complexity of Linear Search?
The time complexity is O(n) in the worst case, where n is the number of elements in the array.
Q4. Can Linear Search find multiple occurrences of a number?
Yes, it can be modified to return all indices where the number occurs.
Q5. Why should beginners learn Linear Search?
It introduces basic algorithmic thinking, iteration, recursion, and array handling, which are foundational skills in programming.
Conclusion
Linear Search is a simple yet essential searching algorithm that every beginner should learn. It teaches fundamental programming concepts like loops, condition checking, recursion, and working with arrays.
Practicing Linear Search helps beginners develop problem-solving skills, understand different approaches to searching, and build confidence in implementing algorithms in JavaScript. By experimenting with single and multiple occurrences, different loop structures, and recursion, learners can strengthen their foundation for more advanced algorithms.




