-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2 Let.js
More file actions
52 lines (38 loc) · 1.26 KB
/
2 Let.js
File metadata and controls
52 lines (38 loc) · 1.26 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
/*
The let keyword was introduced in ES6 (2015).
Variables defined with let cannot be Redeclared.
Variables defined with let must be Declared before use.
Variables defined with let have Block Scope.
*/
let a='rana';
console.log(a);
//var a; SyntaxError: Identifier 'a' has already been declared
//*************Block Scope****************
let x=9;
//let x=10;SyntaxError: Identifier 'x' has already been declared
{
let x=10;//Redeclaring let Variables
console.log(x);
}
console.log(x);
var c=4;
console.log(c)
{
var c=5;//Variables declared with the var keyword can NOT have block scope.
console.log(c);
}
console.log(c);//Variables declared inside a { } block can be accessed from outside the block.
// *****************Hoisting*******************
/*
Variables defined with var are hoisted to the top and can be initialized at any time.
Meaning: You can use the variable before it is declared:
*/
carname='crola';//**************hoisting***************
console.log(carname);
var varname;
/*
Variables defined with let are also hoisted to the top of the block, but not initialized.
Meaning: Using a let variable before it is declared will result in a ReferenceError:
*/
rana=23; //ReferenceError: Cannot access 'rana' before initialization
// let rana;