所谓的块级作用域:形成一个暂时性的死区;{ }
一、共同点:
a:都是用来声明变量;
b:都能形成一个块级作用域;
c:都只能在声明变量的块级作用域里面有效;
二、不同点:
1.let:
a:在同一作用域里面不能使用let声明多个相同变量;
b:变量不会泄露在全局;
c:let声明的变量不会进行变量的提升;
2.const:
a:也不会进行变量的提升;
b:const常用来声明一个常量;用const声明的变量的值时不能改变的!!!
如果用const声明变量的话-----Uncaught TypeError: Assignment to constant variable.(报错)
<body> <script> for(const i=0;i<10;i++){ //Uncaught TypeError: Assignment to constant variable. console.log(i); } for(let i=0;i<10;i++){ //正常输出! console.log(i); } </script> </body>