FAQ: Scope - Practice Good Scoping

Every function is its own namespace. Variables defined with var are confined to the namespace in which they are declared. They are said to shadow variables of the same name in outer scope. That is how it was in ES5.

The concept of block scope was introduced in ES2015 and gave us the const and let declarations that apply inside blocks, treating them as their own namespace, as well. While this may offer us the convenience of re-using variable names, it also affords the ability to keep names simple since they are well enough constrained that they can’t be misconstrued if we understand the nature and behavior of the block they are in. So, a, b, c and x and y, and m and n or i, j, k are all suitable variables.

In outer scope we are dealing with more defined objects that are accessed from all over the program whose names need to impart meaning so a reader will know their makeup and usage in the overall program.

So does that mean that if I use var for variable assignment that the block scope won’t function if I’m not using let or const?

var declaration only gives function scope, but not block scope. Only let and const have block scope.

Hi, i have a question about variable renaming. it is said in the lesson that “A better practice would be to rename the variable inside the block.”
But isn’t it less optimal when use too much variable names ?