const命令用來(lái)聲明變量,但是與let不同的是const聲明的是常量。一旦聲明,常量的值便不可更改
const PI = 3.14159265857;console.log(PI);//3.14159265854PI = 3;console.log(PI);//3.14159265854const PI = 8;console.log(PI);//報(bào)錯(cuò) Uncaught SyntaxError: Identifier 'PI' has already been declared
const的作用域與let命令相同:只在聲明所在的塊級(jí)作用域內(nèi)有效。
if (condition) { const MAX = 5;}// 常量MAX在此處不可得//報(bào)錯(cuò) Uncaught ReferenceError: condition is not defined
const聲明的變量同樣不可以重復(fù)聲明
var message = "hello";let age = 25;//以下兩行代碼都會(huì)報(bào)錯(cuò)const message = "goodbye";const age = 30;