Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Can you push to a const array in JavaScript?

Do you know what will be the output of this code?

JavaScript
const arr = [1];
arr.push(2);
console.log(arr);
Show answer and explanation

The answer is [1, 2].

const only stops arr from pointing to a different value. The array itself can still be changed. Use Object.freeze to make it read-only.

Take the full quiz Next question →

More Basics questions

  1. Why does [33, 2, 8].sort() not sort numbers correctly?
  2. What happens when you assign past the end of a JavaScript array?
  3. Why does [1, 2, 3].map(parseInt) return [1, NaN, NaN]?
  4. What does Array.push() return in JavaScript?
  5. How do you remove duplicates from an array with Set?

Related topics