Global vs Local Variables


#1

I understand the mechanics of the lesson here. My question is more philosophical.

What is the advantage to giving a local variable the same name as a global variable? Wouldn't that increase confusion on the part of the humans?


#2

idk whats' the actual advantage, i do know that with good spacing and format aka the look of the whole code, you wouldn't be all that confused. Or maybe its just me, i don't find it hard to distinguish them two since local variables are always inside { } <----these


#3

i read some articles, well i think the main advantage is to (on contrary to what you said actually) it kinda makes things easier to read, or rather code. I mean the more variables you have the more confusing it is.


#4

What is the advantage to giving a local variable the same name as a global variable?

The purpose of teaching you local and global scope will come into handy when you start writing large
projects in javascript. You may not see the importance now because you're only writing small pieces of code at the moment.

Take for example, you are writing an algorithm that takes almost 300 lines of code. There's the possibility of falling into the error of having named 2 variables the same and this will affect your program. To solve this you have to understand how to use scope.

Hope this was helpful.


#5

soooo how to understand user code? im kinda of a noobie


#6

" user code?"
i apologize that was a typo i meant "use scope"
as in ( global and local);


#7

Known as shadowing, prevents your function from seeing or accessing the global unless addressing the property, directly.

    var myVar = "defined";
    var myFuncNoShadow = function () {
        console.log(myVar);              // defined
    };
    var myFuncWithShadow = function () {
        var myVar;
        console.log(myVar);              // undefined
        console.log(window.myVar)        // defined
    };

This assumes there is a real need to have the global, and shadowing prevents our function from modifying it, essentially protecting it.

Why would we use the same variable name in multiple places? One reason is semantics. It may be the variable name is the best one to describe what it refers. A program is usually an integration of several modules. When reading a module, it's important that we get the right impression of what is what within the function.

Variables perform a role, and many of these roles are programmatic and typical in many functions. Using the same names to describe these roles makes the most practical sense.

The conclusion, then is to use local variables as much as possible and only use globals when there is a need to be available throughout the program. Nothing has been mentioned with regard to advantage. It's not about advantage. Protect those globals.


#8

so basically to sum what you are saying: try to use local variables as much as possible, and that global variables are for convenience in coding right??


#9

I wouldn't go so far as to say that. They are not for convenience, but purposefully intended, or they shouldn't be there. Global variables are vulnerable. There is only one namespace in JavaScript. This means all the programs are running in the same environment. If we have a module (plug-in) that is written by another author that just happens to have the same variable (global) as we do, they will conflict. Either our program or the other, or both will not run as expected.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.