Business scenario: Today, I want to retrieve the values of several parameters from the regular expression array object. I find that many people use const variable declaration.
Scene:
Const, var, let; const, var, let;
1. Variables defined by const cannot be modified and must be initialized.
1 const b = 2; // const b; // error, must initialize 3 console.log(' const definition b: '+ b); // b = 5; 5 // console.log(' const definition b: '+ b); // Cannot outputCopy the code
2. The variable defined by var can be modified. If it is not initialized, undefined will be output.
1 var a = 1; 2 // var a; // 3 console.log(' var defines a: '+ a); Function change(){5 a= 4; 6 console.log(' var definition a: '+ a); A =4 7} 8 change(); 9 console.log(' var defines a as the internal value of the function: '+ a); // a=4Copy the code
3. Let is a block-level scope. If let is defined inside a function, it has no impact on the outside of the function.
1 let c = 3; 2 console.log(' let define c: '+ c); 3 function change(){4 let c= 6; 5 console.log(' function let definition c: '+ c); C =6 6} 7 change(); 8 console.log(' let definition c after function call is not affected by function internal definition: '+ c); / / output c = 3Copy the code
These are the main differences. In general, many people prefer to use VAR more.