当前位置:网站首页>let和var的区别面试题答案
let和var的区别面试题答案
2022-04-22 12:25:00 【云墨-款哥的博客】
这里对之前的一篇博客做出解释
博客地址:let和var的区别,几个小面试题
第一题:
console.log(dog); //undefined
var dog = "小狗"
console.log(cat); //Error: Cannot access 'cat' before initialization
let cat = "小猫"
解释:
第一个输出,是因为var声明的变量会产生变量提升,简单来说就是变量经过预解析的过程,会先被赋值为undefined。
第二个报错,是因为let声明的变量不存在变量提升
第二题:
第1组
var dad = '我是爸爸!'
console.log(dad); //我是爸爸!
var dad = '我才是爸爸!'
console.log(dad); //我才是爸爸!
第2组
let son = '我是儿子!'
console.log(son); //我是儿子!
let son = '我才是儿子!'
console.log(son); //Error: 'son' has already been declared
解释:
第一组可以正常输出,是因为var声明的变量可以重复声明
第二个报错,是因为let声明的变量不允许重复声明
第三题
{
let a = 1;
{
// 块级作用域
let b = 2;
console.log(a); // 1
}
console.log(b); // b is not defined
}
解释:
第一个输出,是因为当前块级作用域没有a这个变量,会自动向上一级作用域查找,上级作用域中的a被赋值了1
第二个报错,是因为let声明的变量会自动生成块级作用域,b是块级作用域声明的变量,外层作用域无法访问
第四题
for (let i = 0; i < 3; i++) {
console.log(i); // 1 2
}
console.log(i); //Error: i is not defined
解释:
第一个输出,是因为for循环里let声明的变量会自动生成块级作用域,i是块级作用域声明的变量,在该块级作用域内可以正常访问
第二个报错,是因为超出了i所在的块级作用域
第五题
第1组
var monkey = '我是美猴王!';
{
console.log(monkey); // 我是美猴王!
var monkey = '我是六耳猕猴';
}
console.log(monkey); //我是六耳猕猴
第2组
let monkey = '我是美猴王!';
{
console.log(monkey); // Error: Cannot access 'monkey' before initialization
let monkey = '我是六耳猕猴';
}
console.log(monkey); //我是美猴王!
解释:
第一组可以正常输出,是因为var声明的变量不存在块级作用域
第二个报错,是因为ES6规定当我们在一个块级作用域中使用let和const来声明变量时,这个变量一开始就会形成一个封闭的作用域,也就是说即使向上的作用域中存在这个同名变量我们也无法访问到
这里要注意区分在函数作用中var 与let的区别,代码如下
<script> let a = 1; (function () {
console.log(a); //这里会报错 let a = 2; })(); </script>`
版权声明
本文为[云墨-款哥的博客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_56026872/article/details/118001518
边栏推荐
- [concurrent programming 051] implementation principle of volatile memory semantics
- 【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
- Some problems in the use of zuul
- LeetCode 695、岛屿的最大面积
- NER简单综述
- Synchronized lock and its expansion
- Design and implementation of house leasing system based on J2EE Rar (paper + project source code + database file)
- "Stack overflow Chinese content" is finally here! Invite you to experience feedback and get a good gift
- Electrician Lecture 2
- JS输出随机数
猜你喜欢

腾讯云域名绑定

Introduction of a protection circuit of heat pump device with automatic switching overcurrent protection module (application case of acs758 / ch704)

电工第二讲

PyTorch处理RNN输入变长序列padding

Codeforces Round #783 (Div. 2)

Remote monitoring system of greenhouse based on stm32f103c8t6 + esp8266

LeetCode 83、删除排序链表中的重复元素

Free trial for the first three months! Borui data alarm platform onealert is in progress

Practical tutorial | pytoch - minimalist implementation of elastic training (with source code)
![[deeply understand tcallusdb technology] insert data into the specified location of the list interface description - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[deeply understand tcallusdb technology] insert data into the specified location of the list interface description - [list table]
随机推荐
What role does RF chip play in mobile phone?
LeetCode 34、在排序数组中查找元素的第一个和最后一个位置
3D应用旋转相册
The ordering system breaks the bottleneck period of wholesale enterprises and helps enterprises with digital transformation
[concurrent programming 054] multithreading status and transition process?
[deeply understand tcallusdb technology] delete all data interface descriptions in the list - [list table]
JS基础10
Synchronized lock and its expansion
Electrician Lecture 2
LeetCode 118、杨辉三角
The higher the refresh rate, the better?
【并发编程054】多线程的状态及转换过程?
MySQL 5.0 installation tutorial illustration detailed tutorial
ROS2学习笔记(十)从turtlesim学习ROS2的工作空间
[concurrent programming 049] talk about the classification of reordering?
Concept and operation of stack
. net treasure API: outputformatter, format output object
Codeforces Round #783 (Div. 2)
【深入理解TcaplusDB技术】将数据插入到列表指定位置接口说明——[List表]
ESP32-CAM使用历