-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariables.js
122 lines (95 loc) · 3.18 KB
/
variables.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//* if any varib/any fuction is accessed without declaration even assigns a value afterwards, it will throw an error: not defined
//* in other words, accessing any variable/func without declaring or assinging any value throws an error.
//console.log('a = ' + a); // Uncaught ReferenceError: a not defined
//console.log('foo = ' + foo()); // Uncaught ReferenceError: aFunc not defined
//console.log('m = ' + m); //Uncaught ReferenceError: m is not defined
//m= 10;
//* hoisting - var/function declarations are processed before any code execution.
console.log('x = ' + x); // undefined
var x;
console.log('y = ' + y); // undefined
var y=1;
console.log('bar = ' +bar()); bar = bar
function bar(){
return "bar";
}
//* assigning a value to a variable without declaration anywhere, JS runtime will a global variable with that name in execution
(function(){
g=10;
})();
console.log('g = ' + g);
gFunc();
function gFunc(){
gg = 30;
}
console.log('gg = ' + gg);
//* hoisting - not applicable for let/const variables throws an error
//console.log('z = ' + z); // Uncaught ReferenceError
//let z=1;
//console.log('w = ' + w); // Uncaught ReferenceError
//const w=1;
//* Redeclaration possible for var declarations even in strict mode
var a = 1;
var a = 4;
console.log( 'a = '+ a); // a= 4
//*** Variable redeclaration not possible for let and const even with var / function name throws syntax error , stops the page to start execute)
//let b = 1;
//let b = 4;
//console.log( 'b = '+ b); // Uncaught SyntaxError
//let u = 1;
//var u= 4;
//console.log( 'u = '+ u); // Uncaught SyntaxError
//function u(){}
//* Default values of var,let - undefined
var e;
let f;
console.log( 'e = '+ e);
console.log( 'f = '+ f);
//*** Constant variable declaration without initialization throws an Syntax error and stops the page to start execution itself
//const g; // Uncaught SyntaxError
//* constant variables can't be changed once initialized,
//* re-assigning new value to constant variable throws an error
//const p = 12;
//p = 24; // Uncaught TypeError
//console.log( 'p = '+ p);
//* constant array/obj can be created and can be changed inside values but not const variable( throws an error)
const arr = [1,2,3,4,5];
console.log('arr = '+ arr);
arr[0] = 100;
console.log('arr = '+ arr);
const obj = {
name : 'suresh'
}
obj.name = 'ganesh';
console.log('obj = '+ obj.name); // obj = ganesh
//arr = [23]; // Uncaught TypeError
//console.log('arr = '+ arr);
//* constant function can be created
const pFunc = function(xx,yy){
return xx+yy;
}
console.log(pFunc(1,2));
console.log(pFunc(5,4));
//* var variables have function scope , let variable have block scope
//Ex1
function f1(){
var v1 = 10;
}
f1();
//console.log('v1 = ' + v1); // Uncaught ReferenceError v1 not defined
//Ex2
var v2 = 10
function f2(){
var v2 = 20;
console.log('v2 = '+ v2); //v2 = 20
}
f2();
console.log('v2 = '+ v2); //v2 = 10
//Ex3 for let variable
function f3(){
if(true){
let v3 = 20;
}
//console.log('v3 = ' + v3); // Uncaught ReferenceError: v3 is not defined
}
f3();