repl.it
@cmclau01/

Hack Reactor JS Function Hoisting & Scope workshop

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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*Function hoisting describes a phenomenon in JS where when code is read by the JS engine it brings variables and function declarations to the top of their scope before the rest of the code is read by the interpreter.

Scope is the area where the bind between variables and their values is valid. Scope and hoisting are very closely related.
*/
/*var msg = 'Holy cow this is interesting';
console.log(msg);

var f = function () {
  console.log(msg);
  var msg = 'Not as interesting as it is oveer here';
}

//What is actually happening is: 
/* 
var msg = 'Holy cow this is interesting';

console.log(msg);

var f = function () {
  var msg;
  console.log(msg);
  msg = 'Not as interesting as it is oveer here';
  console.log(msg);
}

Get into the habit of console.logging everything so that you can work your way through what you're writing. It takes an unlimited number of arguments e.g. console.log('1st F LOG!', msg);

**Declarations are hoisted, but values are not**/

/*Function declarations override variable names when they habe the same name, just an odd quirk. e.g.: */
var myName;

function myName () {
  console.log('Hack Reactor');
}
myName();

//Lesson: declare variables at the top of all scopes as its considered a best practice;




















Native Browser JavaScript
?