+ 2
[SOLVED] what is the significance of "const" and "let" keyword in the following code?
const x=15; for(let x=0;x<=10;x+=2){ y=x;} alert(y);
4 Respostas
+ 5
The 'const' and 'let' variable declarations are part of ES6, or ECMAScript 6, the latest edition of the scripting-language specification that, among others, standardizes JavaScript. Basically, it's newer and shinier JavaScript. It is not in wide use yet, but it's getting more and more popular. I think it's great for use with nodejs.
Basically, 'let' and 'const' work much like 'var', except they cannot be declared again, among ther things. You can still use 'var' in ES6, but there's not much advantadge to it.
Scope (where the variable is available to use) is one major difference. 'Var' variables are either function scoped, when declared inside a function, or globally scoped. 'Const' and 'let', on the other hand, are block scoped. Blocks are anything with curly brackets; functions, 'if' statements, 'for' loops and so on, so it can be used with greater specificity.
In ES6, 'let' should replace 'var' as the default declaration. You can update a 'let' variable just like you normally would.
'Const' is a constant reference to a value. Variables created by 'const' are immutable (can't be changed), as the identifier cannot be reassigned. If a constan refers to an oject, its properties can still change.
´´´
let exp = 10;
let exp = 20; // Not allowed!
exp = 20; // Works
const password = '12345";
password = '54321'; // Not allowed!
const user = {
username: 'Username1'
}
const user = { username: 'Username2' } // Not allowed!
const user.usernane = 'Username2'; // Works
´´´
Here's a good article on variable declarations: https://strongloop.com/strongblog/es6-variable-declarations/
For a good overview of the changes, see: http://es6-features.org
The full specification can be found at: https://www.ecma-international.org/ecma-262/6.0/
+ 5
https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75
+ 2
Const tells the compliler that the variable is a constant and so can't be change after it has been defined.
Let makes a variable with a local scope.
+ 1
thanks... I got it now...