How to find the index of a value in an array in JS?

JavaScript: How to find the index of a value in an array in JS?

If you have an array of values, and you want to know what index a certain value has, you can use this:

  1. let fruits = ["apple","banana","orange","grapes"];
  2. console.log(fruits.indexOf("orange")); //returns 2, as it is the 3rd item in array but array index starts at 0

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to find the index of a value in an array in JS?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the JavaScript language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to set environmental variables in your .htaccess file

What is the loop variable in a @foreach loop in Blade?

How to eager load multiple levels of relationships in Eloquent?

How to add a name prefix to all routes in a group?

How to add the CSRF (Cross-site request forgery) token in Laravel?

Push vs Pop vs Shift vs Unshift

How to set the Expires header in .htaccess

What are the SPL Iterators in PHP?

A list of operators on Eloquent's where() method

How to install and use optipng on Linux CentOS