Linear Search Algorithm in JavaScript

Linear Search Algorithm in JavaScript


let myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function linearSearch(arr, element) {

  for (let i = 0; i < arr.length; i++) {

    if (arr[i] === element) {

      return `Element ${element} found in index ${i}`;

    }

  }

  return `Element ${element} is not found`;
}

console.log(linearSearch(myArray, 5));

 

About Author

Photo

Hi, I am Mahbub Hasan
Software Engineer

I am very much interested to share my programming
and development knowledge with the people.I will try
to update my blog everyday with new technology inshallah.

Thank You

For Visiting My Blog.

Contact Me