【前端学习】—let const var之间的区别(十三)

【前端学习】---let const var之间的区别(十三)

一、let const var之间的区别

二、代码演示

bash 复制代码
   <script>
        // 1、const let不存在变量提升 var 存在变量提升

        //var
console.log(`fullName`,fullName);//fullName undefined
//var fullName='cai';
/*
var 声明变量的方式会存在变量提升,var在声明变量之前,首先会将var fullName提升到代码块的最前面

*/



//let
console.log(`fullName`,fullName);//Uncaught ReferenceError: Cannot access 'fullName' before initialization

let fullName='cai';

//const
//console.log(`fullName`,fullName);
//const fullName='cai'//let const.html:13 Uncaught ReferenceError: Cannot access 'fullName' before initialization

    </script>
bash 复制代码
var fullName='cai';
function f(){
    console.log(`fullName`,fullName)//undefined
    if(false){
       var fullName='kiki'
    }
}

f();
bash 复制代码
let fullName='cai';
function f(){
    console.log(`fullName`,fullName)//cai  全局作用域
    if(false){
        //块级作用域 只在当前范围内生效
       let fullName='kiki'
    }
}

f();
bash 复制代码
// 暂时性死区 声明变量之前不能提前给变量赋值

var fullName='cai'
if(true){
  fullName='test'
  let fullName;//声明变量之前不能提前给变量赋值
}
bash 复制代码
//不能重复声明
function f(){
    let a='10';
    let a='20';
}
bash 复制代码
var fullName='cai'
console.log(`window`,window.fullName);//cai
//let 和const声明的变量不会挂载在window对象下面

const fullName1='cai'
console.log(`window`,window.fullName1);// undefined
bash 复制代码
//const声明的基本类型变量的值不能被改变,引用类型可以
const familyName='cai';
//familyName='cai1'
console.log(familyName)//Uncaught TypeError: Assignment to constant variable.


const fruit=['apple'];
fruit[0]='orange';
console.log(fruit);//['orange']
    
相关推荐
GFCGUO9 分钟前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
丝丝不是土豆丝2 小时前
学习 CSS 新的属性 conic-gradient 实现环形进度条
学习
S hh2 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习
万叶学编程2 小时前
Day02-JavaScript-Vue
前端·javascript·vue.js
wusam2 小时前
螺蛳壳里做道场:老破机搭建的私人数据中心---Centos下Docker学习04(环境准备)
学习·docker·centos
攸攸太上2 小时前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
Geek之路3 小时前
QT系统学习篇(1)
开发语言·qt·学习
IFTICing3 小时前
【文献阅读】Attention Bottlenecks for Multimodal Fusion
人工智能·pytorch·python·神经网络·学习·模态融合
新手unity自用笔记4 小时前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#