FAQ: Iterators - The .findIndex() Method

This community-built FAQ covers the “The .findIndex() Method” exercise from the lesson “Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Create a Back-End App with JavaScript
Create a Front-End App with React
Create Video Games with Phaser.js
Full-Stack Engineer
Front-End Engineer
Back-End Engineer
Software Engineering Foundations
Building Interactive Websites
Building Interactive Websites

Iterators
Learn JavaScript
Create a Professional Website with Velo by Wix
Velo by Wix: Working with Data
Learn JavaScript: Iterators

FAQs on the exercise The .findIndex() Method

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I noticed on this lesson that in the example provided:

const jumbledNums = [123, 25, 78, 5, 9]; 

const lessThanTen = jumbledNums.findIndex(num => {
   return num < 10;
});

console.log(lessThanTen);

a return is used within .findIndex. Is this because it’s on multiple lines? So if we did the following instead:

const lessThanTen = jumbledNums.findIndex(num => num < 10);

no return is needed?

I also tried testing the multi-line example provided by taking out the return and it logged -1 to the console, which indicates .findIndex did not find an item that evaluated to true. Why is -1 logged to the console instead of undefined when return is removed?

When return is removed from the callback (assuming there are body braces) then .findIndex() doesn’t see the return value of each iteration. Once the iteration is complete, it returns the only thing it can, -1.

That is the concise body method of writing a callback. The return is implicit when no braces are used.

Ah, this makes sense. Since a function is passed as the parameter, every iteration is returning undefined when the return is not included but it’s returning undefined to .findIndex() which is then returning -1 once it completes iterating because nothing evaluated to true.

Thanks!

1 Like