forked from ramana4029/es6-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlet-const.js
More file actions
39 lines (29 loc) · 792 Bytes
/
let-const.js
File metadata and controls
39 lines (29 loc) · 792 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
39
//Let Scoping rules
function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable!
console.log(x); // 2
}
console.log(x); // 2
}
varTest();
function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log(x); // 2
}
console.log(x); // 1
}
letTest();
//Cleaner code in inner functions
//Ex: for loop using a
// SyntaxError for redeclaration.
//When used inside a block, let limits the variable's scope to that block
//Constants are block-scoped, much like variables defined using the let statement.
//The value of a constant cannot change through re-assignment, and it can't be redeclared.
// define myconst as a constant and give it the value 7
const myconst = 7;
// this will throw an error
//myconst = 20;