Contents
  1. 1. JavaScript变量声明之var/let/const
    1. 1.1. const
    2. 1.2. var
    3. 1.3. let

JavaScript变量声明之var/let/const

const

const a; 错误❌的声明方式!!!

const声明的变量必须要初始化!

e.g. const a = 1; or const a = '';

var

var定义的变量可以不用初始化(值为undefined), 且不会报错;var定义的变量其值可以被修改!

let

let 定义的变量拥有作用域,在函数内部定义let级变量,则其不会改变函数外部变量属性和值;

1
2
3
4
5
6
7
8
9
let b = 2;
function test(){
let b = 222;
console.log("inner value: " + b);
}
console.log(b); //2
test(); //inner value: 222

Creative Commons License
本文由Lomo创作和发表,采用Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License进行许可.
转载请注明作者及出处,本文作者为Lomo,本文标题为js-var_let_const.
Contents
  1. 1. JavaScript变量声明之var/let/const
    1. 1.1. const
    2. 1.2. var
    3. 1.3. let