Control structures Variable Declarations Revisited
Variable Declarations Revisited
•
Variables can be declared (within a block context) using one of the
following statements:
let variable1 , variable2 , ...
let variable1 = value1 , v a r i a ble2 = 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 renc eErro r : i is not defined
Ref e renc eErro r : j is not defined
COMP284 Scripting Languages Lecture 11 Slide L11 – 15