Introduction

Searching, in the context of Computer Science, is the process of locating a particular element in the given list/array. If we pay close attention, we can find search algorithms everywhere.

Consider the process of logging into a website. Entered email and password are searched against the existing key-value pairs in the database to validate the user.

In this article, let us look at the most basic algorithm to search through a given list of elements - Linear Search.

Understanding Linear Search

The Linear Search algorithm is a set of instructions to traverse the given list and check every element in the list until we find whatever element we are looking for. The technical term for the element we are looking for is - key.

The algorithm goes from the leftmost (or starting) element and continues searching until it either finds the desired element or goes through all the elements in the list.

If the element is found, we will return the position (or index) of the element. If the element we are looking for doesn’t exist in the list, we generally return -1.

#javascript #algorithms

Linear Search in JavaScript
1.15 GEEK