Navigate back to the homepage

indexOf() Method in Javascript

Parimal Ghadiyali
October 18th, 2019 · 1 min read

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.


1array.indexOf(item, start)

Example 1

1const array = ["Red", "Orange", "Yellow", "Olive", "Green"];
3console.log(array.indexOf("Red")); // Output: 0
4console.log(array.indexOf("Pink")); // Output: -1

Example 1

1const array = ["Red", "Orange", "Yellow", "Olive", "Green", "Red"];
3console.log(array.indexOf("Red", 2)); // Output: 5
4console.log(array.indexOf("Pink")); // Output: -1

Hope you find this helpful.

More articles from MeshWorld

Install Postman native app in Ubuntu

Install Postman native app in Ubuntu. Simplify workflows and create APIs faster with Postman, a collaboration platform for API development.

October 13th, 2019 · 1 min read

includes() Method in JavaScript

Javascript array includes() is an inbuilt function determines whether the array contains the specified element or not returning boolean value.

June 24th, 2019 · 1 min read
© 2017–2020 MeshWorld
Link to $ to $ to $