• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Rgzs
博客园    首页    新随笔    联系   管理    订阅  订阅
const (常量的使用)

const 声明常量

    1、不能重复声明常量名 
// var num = 10;
    // const num = 15;
    // console.log(num) //Identifier 'num' has already been declared

 

    2、声明后必须赋值 字符串,数字 布尔值等基本数据类型 
// var num1;
    // const num1 = 20;
    // console.log(num1); // Missing initializer in const declaration

   

 

    3、常量特点不能修改;(Assignment to constant variable)

 // const person  = { num:10,age:20} 
    // // console.log(person.num,person.age)
    // person.num = 40;
    // console.log(person.num) //40

    // person = {num:100};
    // console.log(person.num)  //Assignment to constant variable.

    // const arr = [1,2,3,4];
    // arr[1] = 20;
    // console.log(arr)
    // arr = [1,20,3,4]

    // const num  = 10;
    // num = 20;
    // console.log(num);  //Assignment to constant variable.

//
const num = 10; // num = 20; // console.log(num); //Assignment to constant variable.

 

  4、没有变量提升
// function fn(){
    //     console.log(num)
    //     const num=10;
    // }
    // fn()  //Cannot access 'num' before initialization

 

  5、具有块级作用域
for( var j =0;j<5;j++){
        const num = 10;
    }
    console.log(num);//num is not defined

 

posted on 2020-08-31 22:25  飄落的葉子  阅读(243)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3