-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_scopes.js
More file actions
77 lines (72 loc) · 2.48 KB
/
02_scopes.js
File metadata and controls
77 lines (72 loc) · 2.48 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Scopes
//Global Scope & Block Scope
//{} curley braces is scope
//Values declared above {} are availbale as global scope to values inside block as well, but inside block values will not be
let a = 300;
if (true) {
let a = 10;
console.log(`Inner : ${a}`);
}
console.log(a);
//Nested Scopes
function one() {
const username = "Aimon";
}
function two() {
const website = "youtube";
console.log(website); // not username
}
;
// Another
if (true) {
let username = "Aimon";
if (username === "Aimon") {
const website = "VSCode";
console.log(username + " " + website);
}
}
// variable scope
var number1 = 1;
function varkeyword() {
var number2 = 2;
if (number1 + number2 == 3) {
var number3 = 3;
}
console.log(number1);
console.log(number2);
//console.log(number3); throws error
}
varkeyword();
// The number1 is a number that is globally declared. The number2 has the scope of the function it resides in. The number3, even if it’s declared in an if loop, it can be accessed in the function as it’s declared with the var keyword.
//Let Keyword
let numb1 = 1;
function let_keyword() {
let number2 = 2;
if (number1 + number2 == 3) {
let number3 = 3;
}
console.log(number1);
console.log(number2);
// console.log(number3); // Throws error
}
let_keyword();
//The number1 is a number that is globally declared. The number2 has the scope of the function it resides in. The number3 is declared in an if loop, it can be accessed only in the if loop, it’s block-scoped as it’s declared using the let keyword.
// let redeclaration
// let student_name: string = "rachel";
// let student_name: string = "john"; // Throws error
// // Variable cannot be reassigned
// const student_name: string = "rachel";
// student_name = "john";
//Const Variable declaration, The number1 is a number that is globally declared. The number2 has the scope of the function it resides in. The number3 is declared in an if loop, it can be accessed only in the if loop, it’s block-scoped as it’s declared using the const keyword. the const keyword is similar to let but the difference is that const variables can’t be reassigned, variables declared using let can be reassigned.
const numbs1 = 1;
function const_keyword() {
const number2 = 2;
if (number1 + number2 == 3) {
const number3 = 3;
}
console.log(number1);
console.log(number2);
//console.log(number3); // Throws error
}
const_keyword();
export {};