📜  js var vs const - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:43.708000             🧑  作者: Mango

代码示例1
//var variables can be re-declared and updated
    var greeter = "hey hi";
    var greeter = "say Hello instead";
//it becomes a problem when you do not realize that a variable 
//greeter has already been defined before.

//const declarations are block scoped
//const cannot be updated or re-declared
    const greeting = "say Hi";
    greeting = "say Hello instead";// error: Assignment to constant variable. 

    const greeting = "say Hi";
    const greeting = "say Hello instead";// error: Identifier 'greeting' has already been declared