30 Days Of JavaScript

Day 11: Higher Order Functions Part 2

Lesson 1: findIndex

find will give you the element from an array, but what if you just want to find the location of an item that matches a condition and not the item itself?

That's where findIndex comes in, it will tell you the index of the first item that matches your condition. Using the same example as find():

This will log 1 since that's the index of the car with the model of "Juke" (remember that arrays start their index at 0, not 1).

Change the code above to look for a car model that isn't in the list, you'll see it now prints -1. This is JavaScripts odd way of saying "Couldn't find an item"

Outline

Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co