Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.3 — Searching Arrays
JavaScript · Example 1 of 3 ES2016+

indexOf, lastIndexOf and includes

includes is ES2016. "130" (a string) is not 130 (a number). Objects are only equal to themselves.

indexOf and includes look for an exact value, find and findIndex look for the first item that passes a test you write (findLast searches from the end), and some and every answer yes-or-no questions about the whole array.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Given const words = ["apple", "Banana", "cherry"];, print whether any word starts with a capital letter, and the index of the first such word.

02

Write hasDuplicate(arr) that returns true if any value appears twice, using indexOf and some.

Explain it without notes

01

What is the difference between indexOf and includes, including how they handle NaN?

02

When would you use find instead of indexOf?

03

What do some and every return for an empty array, and why?

04

Why does [{ a: 1 }].includes({ a: 1 }) return false?

indexOf, lastIndexOf and includes ES2016+

Console

(nothing was printed)

Expected console output

1
5
5
-1
true
false
-1 true
false