Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
let x = 1;
{
let x = 2;
console.log(x);
}
console.log(x);2 1
- Expecting
2 2, as if the inner line changed the outerx:let x = 2declares a new variable that lives only inside the{}and hides the outer one there. - Expecting
SyntaxErrorfor declaringxtwice: that happens only within the same block; aletin an inner block is a separate variable.
- What does the program print if the inner line is
x = 2withoutlet? - How can code inside the block reach the outer
xonce it is hidden?