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

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


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 install and use optipng on Linux CentOS

What is Agile software development?

How to select what columns to return when calling ::all()

How to convert a string to an array of it's characters in PHP?

How do you access the php://input stream?

How to access the Laravel's route parameters outside of the controller?

How to add and remove items to an array in JS?

How to run an artisan command from a controller

How to set custom HTTP headers in Apache (by editing .htaccess)

What is the spaceship operator (<=>) in PHP?