Concepts

Linear search

Try the free JavaScript compiler

Sequentially check values with a specified equality rule; stop at the first match or return the documented absent sentinel.

Formal Syntax & Structure

javascript syntaxjavascript
findFirst(values, target) → index | -1

Working Example

Example Snippetjavascript
function findFirst(values, target) {  for (let i = 0; i < values.length; i++) {    if (values[i] === target) return i;  }  return -1;}console.log(findFirst([8, 3, 8], 8), findFirst([8, 3], 9));
JavaScript Reference