I thought I can define a var in an if statement and use it later in the following code. Why is this not working in my example?
let a = prompt('1', '');
if (a == 1) {
let x = 10;
let y = 20;
let z = x + y;
} else {
let z = 50;
}
alert(z);
I'm pretty new to JS, thats why this error comes up for me.
What would be the correct way to override z
if the if
is false?