Challenge - 5 Problems
Const Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of const with object mutation
What is the output of the following code?
Javascript
const obj = { a: 1 }; obj.a = 2; console.log(obj.a);
Attempts:
2 left
💡 Hint
Remember, const prevents reassignment but not mutation of object properties.
✗ Incorrect
The const keyword prevents reassigning the variable obj, but the object it points to can be changed. So obj.a is updated to 2.
❓ Predict Output
intermediate2:00remaining
Output of const with primitive reassignment
What happens when you run this code?
Javascript
const x = 5; x = 10; console.log(x);
Attempts:
2 left
💡 Hint
Can you change the value of a const variable?
✗ Incorrect
Trying to reassign a const variable causes a TypeError because const variables cannot be reassigned.
🧠 Conceptual
advanced2:00remaining
Why use const for arrays?
Which statement about const arrays is true?
Attempts:
2 left
💡 Hint
Think about what const protects: the variable binding or the content.
✗ Incorrect
Const protects the variable binding, so you cannot reassign the array variable, but you can change the contents of the array.
❓ Predict Output
advanced2:00remaining
Output of const in block scope
What will this code print?
Javascript
const a = 1; { const a = 2; console.log(a); } console.log(a);
Attempts:
2 left
💡 Hint
Remember const variables are block scoped.
✗ Incorrect
Inside the block, a new const a = 2 is declared, so console.log(a) inside prints 2. Outside the block, the original a = 1 remains, so console.log(a) prints 1.
❓ Predict Output
expert2:00remaining
Output of const with destructuring and reassignment
What is the output of this code?
Javascript
const [x, y] = [1, 2]; x = 3; console.log(x, y);
Attempts:
2 left
💡 Hint
Can you reassign a const variable after destructuring?
✗ Incorrect
x is declared as a const variable via destructuring. Trying to reassign it causes a TypeError.