js in const, var, let the difference between the use of

1.const defined variables can not be modified, and must be initialized.

1 const b = 2; // correct
2 // const b; // error must be initialized
3 console.log ( 'const definitions outside the function b:' + b); // the output value
4 // b = 5;
5 // console.log ( 'const modify definitions outside the function b:' + b); // can not be output
variable 2.var defined may be modified, if no initialization output undefined, no error.


Var A =. 1. 1;
2 // var A; // not given
3 console.log ( 'function definition outer var a:' + a); // can be output. 1 = A
. 4 Change function () {
. 5 A = . 4;
. 6 the console.log ( 'the function defined var a:' + a); // can be output. 4 = a
. 7}
. 8 Change ();
. 9 the console.log ( 'var defined function call after a modification value as a function of the internal : '+ a); // may output a = 4

3.let block-level scope is, let the internal function definitions used, no effect on the external function.


Let C =. 3. 1;
2 the console.log ( 'external function definition let c:' + c); // output. 3 = C
. 3 Change function () {
. 4 let C =. 6;
. 5 the console.log ( 'let the function definition of c: '+ c); // output. 6 c =
. 6}
. 7 Change ();
. 8 the console.log (' defines the internal function is not affected by the definition of the function call let c: '+ c); // output c = 3
----------------
Disclaimer: This article is the original article CSDN bloggers "cicada sitting up" and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_36784628/article/details/80966826

Guess you like

Origin www.cnblogs.com/feiwenstyle/p/11604638.html