Three ways to declare variables in js. const, var, le

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.

 var A =. 1 ;
  // var A; // no error 
console.log ( 'outer var function definition A:' A +); // can be output. 1 = A 
 function Change () { 
 A =. 4 ; 
 the console.log ( 'within the definition of a function var:' + 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

 

Guess you like

Origin www.cnblogs.com/licz/p/11323062.html