Understanding global/local scope 10/13


#1


https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661


"Using my_number without the var keyword refers to the global variable that has already been declared outside the function in line 1. However, if you use the var keyword inside a function, it declares a new local variable that only exists within that function."

I'm not fully understanding this statement. Can someone give me a detail explanation on why this lesson wants you to put var in front of my_number = *2;


var my_number = 7; //this has global scope

var timesTwo = function(number) {
// i don't fully understand why i need to or don't need to put var in front of my_number = number *2; i went back to try it with out var and it still worked.
  var  my_number = number * 2;
    console.log("Inside the function my_number is: ");
    console.log(my_number);
}; 

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);


#2

What is logging to the console?


#3

I was a success either way i put it.

...
var my_number = 7; //this has global scope

var timesTwo = function(number) {
// Changes placed below this line.
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);
...
Or
...
var my_number = 7; //this has global scope

var timesTwo = function(number) {
//Changes placed below this line.
my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);
...
What i'm asking is what is the difference between the two solutions.


#4

Global scope for the my_number value means it is assigned 7.

Local scope within the timesTwo function means my_number is assigned the product of the number parameter * 2. So if you called timesTwo(3), 6 would log to the console. Not 14. Only within the timesTwo function, locally.

Back outside the timesTwo function, calling my_number to the console like this:

console.log(my_number)

would print as:

7

because of global scope. Variable my_name remains 7 until it is reassigned a value, such as "bill" or 97.

timesTwo(7)

would print:

14

because of local scope.

THIRD EDIT:

I completely overlooked your question about whether var is or isn't necessary when assigning values to a variable. There's a lot to read in this link but it's worth it:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

The gist of it is, there are times when you can assign values to a variable without using var but occasionally you can't. Doing so will generate an error in your code. So to be safe, err on the side of caution and use var.


#5

man thank you so much i now understand lol thanks!


#6

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