10. Global vs Local Variables


#1

OK, I need more help.

In the code below code:
1. What is the var keyword.?
2. What code is 'inside' the function?
3. What code is 'outside' the function?

var my_number = 2; //this has global scope

var timesTwo = function(number) {
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

can you copy your link?


#3

Sure. It is the assignment code. I want to be sure I understand what it is asking for.
https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661


#4

where I put in the arrow, you are supposed to have var again. since its in the function you have to declare it again.


#5

so the line is
var my_number = number * 2;


#6

OK, so

Q=What is the var keyword:
A = my_number
Is that right?

What about:
Q=What code is 'inside' the function?
A= ?

Q=What code is 'outside' the function?
A= ?


#7

I got it! I had to read it a few times to understand! I wasn't sure what was in/out of the function! We need flashcards!

var my_number = 7; //this has global scope

var timesTwo = function(number) {
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);


#8

I'm not sure what you mean. var basically creates a piece of information that can be used in the program. it can hold words which are called strings in code(var string = "hello"; ), numbers(var number = 5;), and a Boolean which is either true or false(var boolean = true;) . I hope that helps but I don't know exactly what you are asking.

when you make a function the syntax is this
var myFunction = function(){
}
so at first you are creating a variable with var myFunction. and this just tells the computer that there is a new data type called myFunction and the computer doesn't know what it does yet.

then you type
= function
the = is used for assigning a value, and you are giving the variable myFunction the value of a function.

now that the computer knows that it has a function you have to tell it what you want to be able to use inside of it.
you now have var myFunction = function and you add the ().
inside the () is where parameters go, and its just what will be used inside of the function, from outside. kinda like making a door for a previous block of code to be used inside.

so the current code is
var myFunction = function()
and then you add {} at the end.
any code inside of these {} is what runs with the function.
usually you don't have them on the same line as each other and it will look something like this

var myFunction = function(){
console.log("i just made a function");

};
all of the code inside of the curly braces is inside the function and all of the code outside of them is global.

sorry if that was really long but I wanted to make sure I covered the syntax.


#9

The task was: Change line 4 to use the var keyword. Notice that the value of mynumber in the function is now 14 and outside the function is 7._

Using mynumber 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."_

So, I needed to know what the var keyword was to change it. Then I needed to know what was in/out of the function to declare it local or global.


#10

This was key:
any code inside of these {} is what runs with the function.


#11

ok now I understand. sorry I just didn't know exactly how much you wanted to know.


#13

so on line 4 just add var in front of my_number and you should be alright


#14

ya I agree. the classes are awesome and I love codecademy but sometimes the instructions get confusing and mess me up. but don't hesitate to come to the forums and ask about anything. there are tons of people on here who cant wait to help.


#15

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