LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 20

Searching from an index Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const a = [1, 2, 1, 2];
console.log(a.indexOf(1, 1), a.lastIndexOf(2, 2), a.includes(2, 4));
  • A 0 3 true
  • B 2 1 false Correct
  • C 2 3 false
  • D 1 1 true
Correct Answer: Option B

indexOf(1, 1) starts at index 1 and finds the 1 at index 2. lastIndexOf(2, 2) searches backwards from index 2 and finds index 1. includes(2, 4) starts past the end.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links