Header add

A variable or a constant is nothing but a named memory location in which you can store a value.

So having two similar names under single context will create a confusion while accessing the value i.e which value you really want to access when you refer the name of the variable.

JavaScript allows you to have same names as variable names under different scopes. Global scope will provide you access everywhere i.e throughout your program. A function scope will give you access within a function. In other words you can use a similar named variable in global and function scope. There is another scope called block scope which is associated with if, switch conditional statements, for or while looping constructs if you declare a variable or constant using let or constant (ES6) then those variables scope will be within the conditional statements or looping constructs.

accessing variables happens based on their associated scope.

OR In other Way we can say,

A variable name should need to be unique, just like your name must be unique in your family, We are not talking about outside world or in relationships there might be many people with same name but in your family you will be the only one, it can’t be like your elder brother your younger brother all will have same name as your ! just to uniquely identify we name everyone differently. And the same goes with JS also compiler can’t be able to distinguish between same names that’s why we need to name them differently.

Post a Comment

Previous Post Next Post