LearnFrenzy
Sign in Take a free test

Variables: var, let and const · Find the Error · Question 9

Error: using a block-scoped const outside its block Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

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

main.js
const amount = 1500;
if (amount > 1000) {
  const discount = 10;
}
console.log("Discount:", discount);
  • A ReferenceError: discount exists only inside the if block – declare it before the if with let Correct
  • B It prints Discount: 10
  • C It prints Discount: undefined
  • D TypeError: discount is constant
Correct Answer: Option A

const is block-scoped. Declare let discount = 0; before the if and assign inside it.

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