Question 10
Back to Exercise
The code below stops with an uncaught error. Which statement correctly describes the problem?
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!