【前端学习】—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']
    
相关推荐
CappuccinoRose14 分钟前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
天天鸭29 分钟前
前端转全栈,要学会后端看项目的思维
前端·后端·ai编程
oil欧哟32 分钟前
AI Agent 常用操作入门:独立目录、Plan 模式与跨话题交接指南
前端
孟陬44 分钟前
Foobar 技术周刊 #161:AI 录屏员工屏幕训练 SOP,被当做「动物园的猴子」是否触犯隐私?
前端·后端·程序员
1-1=01 小时前
Vue 混入(Mixin)和 2.插件 (Plugins)
前端·vue.js
用户298698530141 小时前
React 前端实现 Excel 与 OpenXML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
-梅1 小时前
linux(番外)线程,锁,条件变量封装;日志与线程池
linux·c语言·前端·c++
Rain的Java大神之路2 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端
星落梦生2 小时前
Tlias前端案例涉及知识点 (14)
开发语言·前端·javascript
Moment3 小时前
27B 的本地模型也能做知识图谱?太强了!😍😍😍
前端·后端·面试