0
Question about const being the standard when doing ES6?
Going back into ES6! Finally gonna finish the class and get the cert for this class. I get why let is useful for ES6 stuff, gotta localize variables. As I go through the lesson, why is constant used instead of var? It just seems like, unless we're talking about constant, we should stick with var. It confuses me is all!
6 Answers
+ 18
Variables that are declared using the 'let' keyword are called as block scoped variables. The block scoped variables behave the same way as the function scoped variables when declared outside a function, that is, they are accessible globally. But when the block scoped variables are declared inside a block, then they are accessible inside the block that they are defined in (and also any sub-blocks) but not outside the block.
 NOTE!
A block is used to group zero or more statements. A pair of curly brackets delimits the block, that is {}.
For example,
let a = 12; //accessible globally
function myFunction()
{
 console.log(a);
 let b = 13; //accessible throughout function
 if(true)
 {
   let c = 14; //accessible throughout the "if" statement
   console.log(b);
 }
 console.log(c);
}
myFunction();
The output of the code is:
12
13
Reference Error Exception
+ 18
Variables declared with 'var' or 'let' can be changed later on in the program, and reassigned. A once a 'const' is initialized, its value can never be changed again, and it can't be reassigned to a different value.
const x = 'test'
We can't assign a different literal to the 'x' const. We can however mutate 'x' if it's an object that provides methods that mutate its contents.
â 'const' does not provide immutability, just makes sure that the reference can't be changed.
â 'const' has block scope, same as 'let'.
⢠https://code.sololearn.com/W70Dsn9iG0da/?ref=app
+ 18
⢠The const keyword
The ES6 'const' keyword is used to declare the read-only variables, that is, the variables whose value cannot be reassigned. Before ES6, the programmers usually used to prefix the variables that were supposed to be constant.
The 'const' keyword was introduced to provide a native protection to the constant variables.
const pi = 3.141;
var r = 2;
console.log(pi * r * r); //Output "12.564"
pi = 12; //throws read-only exception
Here, when we tried to change the value of 'pi', a read-only exception was thrown.
When writing the ES6 code, it is recommended to switch to using the 'let' keyword because it makes scripts more memory friendly, prevents scoping mistakes, prevents accidental bugs, and makes the code easier to read. But if you are already addicted to the 'var' keyword and comfortable using it, then you can still use this.
+ 6
var: Declare a variable, value initialization optional.
let: Declare a local variable with block scope.
const: Declare a read-only named constant.
Heres an example when i would use it.
const pi = Math.PI*2;
pi+=1; < adding 1 to this variable wont work with const but if you change to var it will, thats the diffrence.
+ 1
Thanks. All that effort helps.