'var' is function scoped but 'let' and 'const' are blocked scoped .
example-
//ES5
function driverLisence(passedTest){
if(passedTest){
var name="lokman";
var yearOfBirth=1990;
}
console.log(name+" born in " +yearOfBirth+"have successfully passed the license test");
//name and yearOfBirth will accessible within the body of the function because var is function scoped.
}
driverLisence(true);
//ES6
function driverLisence(passedTest){
if(passedTest){
let name="muna";
const yearOfBirth=1995;
}
console.log(name+" born in " +yearOfBirth+"have successfully passed the license test");
//name and yearOfBirth will not accessible outside of the if block . because they are blocked scoped. 'const' will not changed once initialized.
}
driverLisence(true);
No comments:
Post a Comment