Repository navigation
Expand file tree
/
Copy path01_variable_constant.js
More file actions
87 lines (72 loc) · 2.66 KB
/
Copy path01_variable_constant.js
File metadata and controls
87 lines (72 loc) · 2.66 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
78
79
80
81
82
83
84
85
86
87
var a = 10;
let b = 20;
const c = 30;
console.log(a, b, c);
a = 15;
b = 25;
// c = 35 it shows an error bcz we do not inilise the constant value again
// 2. Block scope example
if (true) {
let x = 100;
const y = 200;
var z = 300;
}
console.log(z); // var scope is global
// console.log(x);
// console.log(y); // these are not assesible .
// 3. changing of const const array/object
const person = { name: "Aman", age: 21 };
person.age = 22;
console.log(person);
// 4. hosting ka difference
console.log(x1);
var x1 = 5;
// console.log(x2) let can not be use before thatr defination
let x2 = 10;
// 5. temporal dead zone
{
//console.log(typeof (x3)); let and var shows the TDZ
let x3 = " hellow ";
}
console.log(typeof x4); // var shows only the undefined it no show the error
var x4 = " world ";
// ---------------- NEW EXAMPLES ----------------
// 6. var can be re-declared, let/const cannot (same scope)
var a = 50; // no error, bas overwrite ho jayega
console.log(a);
// let b = 60; // ❌ Error: Identifier 'b' has already been declared
// 7. function scope vs block scope (var ignores block, let/const respect it)
function testScope() {
if (true) {
var funcScoped = "main function ke andar accessible";
let blockScoped = "sirf is block ke andar accessible";
}
console.log(funcScoped); // ✅ works
// console.log(blockScoped); // ❌ ReferenceError
}
testScope();
// 8. classic interview question: var vs let in loop with setTimeout
console.log("---loop example---");
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log("var i:", i), 100);
}
// output: var i: 3, var i: 3, var i: 3
// kyunki var function/global scoped hai, saare callbacks ek hi 'i' ko share karte hain
for (let j = 0; j < 3; j++) {
setTimeout(() => console.log("let j:", j), 200);
}
// output: let j: 0, let j: 1, let j: 2
// kyunki let block scoped hai, har iteration ka apna naya 'j' banta hai
// 9. const with array — array khud modify ho sakta hai (push/pop allowed)
const arr = [1, 2, 3];
arr.push(4); // ✅ allowed, reference same hai
console.log(arr); // [1,2,3,4]
// arr = [5,6]; // ❌ Error, naya reference assign nahi kar sakte
// 10. accessing undeclared variable vs var before declaration
console.log(typeof undeclaredVar); // "undefined" - error nahi aayega
// console.log(undeclaredVar); // ❌ ReferenceError - typeof ke bina error dega
// 11. global object attachment (var attaches to window/global, let-const nahi)
var globalVar = "main window pe hoon";
let globalLet = "main window pe nahi hoon";
console.log(window.globalVar); // "main window pe hoon" (browser mein)
console.log(window.globalLet); // undefined