Can I access a variable from the outer scope that has the same name as a variable in the current scope?


#1

Question

Can I access a variable from the outer scope that has the same name as a variable in the current scope?

Answer

No, JavaScript does not support this. One way you can try and get around this is either by renaming variables or setting a variable in the outer scope to other variable in the outer scope you’re trying to access. Example:

    	let x = 3;
    	y = x;
    	if(true) {
    		let x = 5;
    		// Prints out 8 (3 + 5)
    		console.log(y + x);
    	}