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?

Upgrade to Pro for quizzes, tracked progress and a completion certificate for just $25 🚀

Want more developer tips, tricks and tools?
Then follow me:
FREE Coding Career Quick Start Guide 🚀
Discover the best way to learn to code, how to land a job and valuable resources to help your journey in this free 15 page value packed guide.
Looking to email me? You can get me on my first name at allthecode.co