[solved] Are vars defined in Function definitions Global or Local?

[solved] Are vars defined in Function definitions Global or Local?
0

#1

I’m doing the Var Scope challenges now and I thought back to this thread and I started to wonder.
e.g.

function minusSeven(num) {
  return num - 7;
}

Is the var num, which is defined in the definition of the function, global or local.
I presume it’s local but who knows right?


#2

It is local to the function. Let’s test it to make sure.

function minusSeven(num) {
  // let's change num's value
  num = num - 7; // num is now 5
  return num;
}

var num = 12;
console.log(minusSeven(num)); // displays 5
console.log(num); // display 12;

The var num = 12; in the global scope is unaffected by the change to num in the local scope of the function.


#3

oh that’s a gd idea lol. I didn’t even think of testing it like that.
Hey, did you run that test in a code editor or does the forum have a way to do that?


#4

You can use an online compiler like repl.it or if you are using Chrome, you can open up the Developer Tools (Ctrl+Shft+J) and then click on Sources --> New Snippet. Then you can enter your code into the Snippet and then either click Ctrl+Enter or the right arrow button at the bottom of the Snippet window to run the code.


[solved] Strings are immutable. Array vars are mutable. But what about String data in an Array?
#5

Holy Shimolies I just tried that and that chrome devTool is awesome.
anyway, yx