showing results for - "function scope and block scope in javascript"
Billie
03 Jan 2019
1/*It's all about the type of following keywords 
2depending upon specific condition.
3'var' is function scope.
4'let' and 'const' are block scope.
5Function scope is within the function.
6Block scope is within curly brackets.For example:*/
7var age = 50;
8var temp=age+10;
9if (age > 40){	
10    var age=temp;
11    console.log(`Your brother ${age} years old than you!`);
12 }
13
14//Output: Your brother 60 years old than you!
15
16/*Note:But if we type console.log(age) outside the block scope than
17the previous value of 'age' with 'var' keyword has been vanished that is:
18
19>console.log(age);
20>60
21
22But in case of 'let' and 'const' keywords the previous value of
23'var' in the previous example is not vanished here's an example:*/
24
25var age = 50;
26var temp=age+10;
27if (age > 40){	
28    let age=temp;
29    console.log(`Your brother ${age} years old than you!`);
30 }
31
32/*Output: Your brother 60 years old than you!
33Note: The output would still same but if we type 'console.log(age)' 
34outside the block scope than the previous value of 'age' with 'var' keyword
35has not been vanished that is:
36
37>console.log(age);
38>50
39
40
41
42
43
44
45