repl.it
@cmclau01/

Global vs Local Scope

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Scope:
-- A scope is created for each function body: read up on lexical scope (built as you type) and execution scope (deals with memory and accessing memory scope through closure). 

//local scope:
*/
/*var func = function () {
  var local = 'scope';
}
console.log(local); 
//will throw error because local is only in the child scope, not the global scope.*/

//global scope:
var globalScope = 'hello there';

function func () {
  console.log(globalScope); //???
  globalScope = 'hello in here';
  console.log(globalScope);
}

func();
Native Browser JavaScript
?