LearnFrenzy
Sign in Take a free test

Arrays · Find the Error · Question 10

Error: pushing to a frozen array Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const COLOURS = Object.freeze(["red", "green", "blue"]);
COLOURS.push("purple");
console.log(COLOURS);
  • A It prints all four colours
  • B It silently ignores the push
  • C TypeError: the frozen array is not extensible, so push throws (even outside strict mode) Correct
  • D SyntaxError
Correct Answer: Option C

Plain assignments to frozen objects fail silently in sloppy mode, but push is a method that checks the result and throws "Cannot add property 3, object is not extensible". Create a new array: [...COLOURS, "purple"].

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