Reprinted from Http://www.cnblogs.com/ksl666/p/5944718.html
Const,var,let Differences in JS
today, for the first time, a const-defined variable was encountered, and the article was collated with relevant Data. The main content Is: JS in three ways to define the variable const, var, let the Difference.
Variables defined by 1.const cannot be modified and must be Initialized.
1 Const B = 2; // correct //3 console.log (' out of Function const Definition B: ' + b '); has output value // b = 5; //
2.var defined variables can be modified, if not initialized will output undefined, will not Error.
1var a = 1; 2 // var a;//will not error 3 console.log (' out of function var definition a: ' + a '); // can output A=14 function change () {5 a = 4; 6 Console.log (' var within the function ' defines A: ' + a '); // can output A=47 } 8 change (); // can output a=4
3.let is a block-level scope, and after the function is defined with let, there is no effect on the outside of the Function.
1 Let C = 3; 2 Console.log (' out of function let ' definition c: ' + C '); // Output c=3function Change () {4 let C = 6; 5 console.log (' in-function let ' definition c: ' + C '); // Output c=6 change(); 8 Console.log (' function call after let definition C is not affected by function internal definition: ' + C '); // output c=3
If there is any mistake, please correct me!
The difference between const, var, let-defined variables in javascript