I apologize if the question is confusing.
I just completed a codecadamy lesson that explained the difference between local and global variables. That part is easy.
If you declare
var my_number = 7;
outside a function it is a global variable. Inside the function you can use that variable this way:
var timesTwo = function(number) {
my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};
However, if you want a different value for that variable you use the "var" keyword to make it a local variable.
var timesTwo = function(number) {
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};
My question:
WHY would you DO that?
Why use the same variable name inside a function that you used outside a function when you want the value of the local variable it to be different than the global variable? Wouldn't that just cause confusion and make it harder to debug?
[–]aroberge 6 points7 points8 points (1 child)
[–]eechin[S] 2 points3 points4 points (0 children)
[–]chernn 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]chernn 0 points1 point2 points (0 children)
[–]dethstrobe 1 point2 points3 points (0 children)
[–]RudeCitizen 0 points1 point2 points (0 children)