js in const, var, let the difference and usage (rpm)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/qq_36784628/article/details/80966826
today first encounter const variable defined, access to relevant information put together this article. The main contents are: js three definitions of the variables in the way const, the difference between var, let's.

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

const b = 2; // correct 
// const b; // error must be initialized 
console.log ( 'outer const function definition b:' + b); // the output value 
// B =. 5; 
// Console. log ( 'const modify definitions outside the function b:' + b); // can not be output

  


2.var defined variables can be modified, if not initialize prints undefined, does not complain.

 

A. 1 = var; 
 // var A; // no error 
console.log ( 'function definition outer var a:' + a); // can be output. 1 = A 
function Change () { 
 A =. 4; 
 the console.log ( 'the function defined var a:' + a); // can be output. 4 = a 
 } 
 Change (); 
 the console.log ( 'after a function call is defined as a function of the internal var modified value:' + a); // can output a = 4

  

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

 

C =. 3 let; 
the console.log ( 'external function definition let c:' + c); // output =. 3 C 
function Change () { 
 let C =. 6; 
 the console.log ( 'let the function definitions c:' + c); // output =. 6 c 
 } 
 Change (); 
the console.log ( 'defines the internal function is not affected by the definition of c let the function call:' + 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/sandea/p/11745412.html