-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (37 loc) · 1.97 KB
/
script.js
File metadata and controls
53 lines (37 loc) · 1.97 KB
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
'use strict';
//** Scope and the Scope Chain */
// Child scopes can reach back to parent scopes, but not the other way around.
//Scoping asks the question - where do variabls live? or where can we access a certain variable, and where not?
// only let an dconst cariable are block-scoped. vaeruable declared with a car end up in the closest function scope
// In javascript, we have lexical scoping, so the rules of where we can access variables are based on exactly where in the code function and blocks are written
// Every scope always has access to all the variable from all its outer scopes. this is the scope chain!
// when a variable is not in the current scope, the engine looks up in the scope chain until it finds the variable its looking for . this is called variable lookup.
// the scope chain is a one-way street. a scope will never, ever have access to the variables of an inner scope.
// the scope chain in a certain scope is equal to adding together all the variable environments of all the parent scopes.
// the scope chain has nothing to do with the order in which function were called. it does not affect the scope chain at all.
function calcAge(birthYear) {
const age = 2037 - birthYear;
function printAge() {
let output = `${firstName}, you are ${age}, born in ${birthYear}`;
console.log(output);
if (birthYear >= 1981 && birthYear <= 1996) {
var millenial = true;
const firstName = 'Steven'; // changed the name from the original as steven is in the current scope.
const str = `Oh, and you're a millenial, ${firstName}`;
console.log(str);
function add(a, b) {
return a + b;
}
// reassinging outer scopes variable.
output = 'NEW OUTPUT!';
// const output = 'NEW OUTPUT!'; makes a new variable
}
console.log(millenial);
console.log(output);
//console.log(add(2, 3)); // can only be seen when strick mode is disabled!
}
printAge();
return age;
}
const firstName = 'Jonas';
calcAge(1991);