LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 8

Writing to index -1 Answer, explanation & discussion

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

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

main.js
const a = [1, 2, 3];
a[-1] = 9;
console.log(a.length, a[-1], a.at(-1));
  • A 4 9 9
  • B 3 undefined 3
  • C 3 9 9
  • D 3 9 3 Correct
Correct Answer: Option D

a[-1] creates an ordinary property named "-1"; it is not an element, so length stays 3. at(-1) still returns the real last element.

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