let和const关键字
let特点
1.在块级作用域里有效(比如循环遍历内部就可以用let定义变量)
{
let a='https://blog.csdn.net/weixin_44255950';
var b='only老K';
console.log(b);
console.log(a);
}
console.log(b);
console.log(a);//a为let定义 只能在代码块中使用
Uncaught ReferenceError: a is not defined
2.不能重复声明
{
let a='https://blog.csdn.net/weixin_44255950';
let a='https://blog.csdn.net/weixin_44255950第二次声明let a';
var b='only老K';
var b='only老K第二次声明var b';
console.log(b);
console.log(a);
}
Uncaught SyntaxError: Identifier ‘a’ has already been declared
3.不存在变量升级
{
let a='https://blog.csdn.net/weixin_44255950';
// let a='https://blog.csdn.net/weixin_44255950第二次声明let a';
var b='only老K';
// var b='only老K第二次声明var b';
console.log(b);
console.log(a);
}
console.log(b);
// console.log(a);//a为let定义 只能在代码块中使用
console.log(c);
var c="only老K";
console.log(d);
let d="aa";
Uncaught ReferenceError: Cannot access ‘d’ before initialization
const特点
1.const关键字用来定义常量