Scope Variable Declarations Revisited
Variable Declarations Revisited
•
Variables can be declared (within a block context) using one of the
following statements:
let va riabl e1 , vari able2 , ...
let variable1 = value1 , var i a b l e 2 = value2 , ...
•
The second statement also initialises the variables
•
Used inside a block, creates a local variable, only accessible within the block
•
Used outside any block, creates a global variable
for ( var i =0; i <1; i ++) {
var j = i + 1
console . log (’I: i =’ ,i , ’ j =’,j)
}
console . log (’O: i =’ ,i , ’ j =’,j)
I: i = 0 j = 1
O: i = 1 j = 1
for ( let i =0; i <1; i ++) {
let j = i + 1
console . log (’I: i = ’ ,i , ’ j =’,j)
}
console . log (’O: i = ’ ,i , ’ j =’,j)
I: i = 0 j = 1
Ref e rence E rror : i is not d efined
Ref e rence E rror : j is not d efined
COMP519 Web Programming Lecture 13 Slide L13 – 12