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

What happens when you reassign a const in JavaScript?

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

JavaScript
const x = 1;
x = 2;
console.log(x);
Show answer and explanation

The answer is TypeError: Assignment to constant variable.

const creates a binding that can’t be reassigned. The code throws before console.log runs. Use let when a variable needs a new value later.

Take the full quiz Next question →

More Basics questions

  1. Why is typeof null "object" in JavaScript?
  2. What is the difference between == and === in JavaScript?
  3. What does Array.push() return in JavaScript?
  4. Is a var loop variable accessible after a for loop?
  5. Why does "5" * "2" equal 10 in JavaScript?

Related topics