-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar-let-diff.html
More file actions
39 lines (31 loc) · 913 Bytes
/
var-let-diff.html
File metadata and controls
39 lines (31 loc) · 913 Bytes
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
<script>
function compareLetVar() {
// Using var
console.log('Using var:');
console.log(a); // Outputs: undefined (due to hoisting)
var a = 5;
console.log(a); // Outputs: 5
if (true) {
var b = 10; // Function-scoped
}
console.log(b); // Outputs: 10 (accessible outside the if block)
// Using let
console.log('\nUsing let:');
// console.log(c); // ReferenceError: Cannot access 'c' before initialization
let c = 15;
console.log(c); // Outputs: 15
if (true) {
let d = 20; // Block-scoped
console.log(d); // Outputs: 20 (accessible within the block)
}
// console.log(d); // ReferenceError: d is not defined
// Re-declaration example
var e = 25;
var e = 30; // No error
console.log(e); // Outputs: 30
let f = 35;
// let f = 40; // SyntaxError: Identifier 'f' has already been declared
console.log(f); // Outputs: 35
}
compareLetVar();
</script>