const命令用来声明变量,但是与let不同的是const声明的是常量。一旦声明,常量的值便不可更改
const PI = 3.14159265857;
console.log(PI);//3.14159265854
PI = 3;
console.log(PI);//3.14159265854
const PI = 8;
console.log(PI);
//报错 Uncaught SyntaxError: Identifier 'PI' has already been declared
const的作用域与let命令相同:只在声明所在的块级作用域内有效。
if (condition) {
const MAX = 5;
}
// 常量MAX在此处不可得
//报错 Uncaught ReferenceError: condition is not defined
const声明的变量同样不可以重复声明
var message = "hello";
let age = 25;
//以下两行代码都会报错
const message = "goodbye";
const age = 30;