ES5 Specifications includes the indexOf() method for Array data structure.
The indexOf() method i used to determines whether an array contains a specific element or not. It returns its position if it exists in an array.
Syntax
array.indexOf(item, start);
Example 1
const array = ["Red", "Orange", "Yellow", "Olive", "Green"];
console.log(array.indexOf("Red")); // Output: 0
console.log(array.indexOf("Pink")); // Output: -1
Example 2
const array = ["Red", "Orange", "Yellow", "Olive", "Green", "Red"];
console.log(array.indexOf("Red", 2)); // Output: 5
console.log(array.indexOf("Pink")); // Output: -1
Hope you find this helpful.
Related Articles
Deepen your understanding with these curated continuations.
Convert array to an object in JavaScript
This article explains simplest and quickest way to convert array to an object in JavaScript. Using widely accepted spread operator `...` makes easy to do it.
How to Update an Array Element in JavaScript
Learn the simplest ways to update array elements in JavaScript. This guide explains how to use assignment operators and modern methods to modify array values.
Flatten an Array One Level Deep in Javascript
This article explains to flatten an array one level deep in javascript comparing with lodash _.flatten method.