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

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


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

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?


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.


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?


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?

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