2.ES6 const常量

ES6中可以使用const声明一个只读的常量。

1、一旦声明,常量的值就不能改变;
2、常量声明就要定义值,否则报错;
3、常量值作用域和let一样;

案例:

  // 1.常量值不可以更改
  const test = '123'
  console.log('test == %s', test);
  test = '456'  // "test" is read-only

  // 2.常量声明就要定义值,否则报错
  const test2;  //Unexpected token
  console.log(test2);

  // 3.常量值作用域和let一样
  if(true){
    const test3 = 'test333'
    console.log('test3 == %s', test3); // 结果:test333
  }
  console.log('test3 == %s', test3);  //test3 is not defined

你可能感兴趣的:(2.ES6 const常量)