<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<script>
// var声明的变量存在变量提升
// 只提升声明,不提升赋值
/*
下面的代码相当于
var a;
console.log(a);
a = 1;
*/
console.log(a); // undefined
var a = 1;
// let声明的变量不存在变量提升,因此会报错
console.log(b); // Uncaught ReferenceError: Cannot access 'b' before initialization
let b = 2;
function func() {
console.log(c); // undefined
var c = 3;
}
func();
console.log(c); // Uncaught ReferenceError: c is not defined
</script>
5、js - 面试 - 变量提升
于 2023-05-24 10:14:41 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)