JavaScript let和const的用途是什么
在本文中,我们将讨论使用let和const在JavaScript中的用法,尽量给出一些理论解释和示例代码。
let: let关键字用于声明JavaScript中的变量,实际上是将其作为块级作用域变量;也就是说,它允许我们在块或表达式中声明变量,而不是在整个文档中。我们不能在它们的块级作用域之外访问let变量。
语法:
let variable_name = value;
以下是上述语法的示例(不可运行):
let name = "GeeksforGeeks";
特点:
- 使用let 声明的变量在JavaScript中不能被重新声明。这意味着如果一个变量使用let 声明,那么在同一上下文中不能再次重新声明相同的变量。
示例 1: 在这个示例中,我们将看到在相同作用域中不能重新声明变量x所导致的语法错误。
HTML
<script>
let x = "GeeksforGeeks";
// Redeclaring same variable
let x = 12;
console.log(x);
</script>
输出:
SyntaxError: Identifier 'x' has already been declared
注意: let关键字允许在一个作用域中声明同一个变量,代码块作用域 { },但它不能在代码块作用域之外使用。
示例2: 在这个示例中,我们将尝试在代码块作用域之外访问let变量,正如前面提到的,我们不能在它的作用域外访问let变量,因此输出会收到一个错误。
HTML格式
<script>
{
let x = 12;
}
console.log(x);
</script>
输出:
ReferenceError: x is not defined
示例3: 在这个示例中,我们将声明一个全局 let 变量,在一个块内部我们将声明另一个 let 变量,然后在该块之外,如果我们尝试打印该值,我们实际上会注意到它采用了全局声明的 let 变量的值。
HTML
<script>
let x = "GeeksforGeeks";
{
let x = 12; // local variable
}
// x takes value of the Global Variable x
console.log(x);
</script>
输出:
GeeksforGeeks
示例4: 我们可以在块级作用域 内声明一个 let 变量,这个变量将被视为块级作用域内的局部变量,并且比之前定义的全局变量更具优先级。
HTML
<script>
let x = "GeeksforGeeks";
let y = "Computer science portal";
{
let x = 12;
console.log(x);
console.log(y);
}
console.log(x);
</script>
输出:
12
Computer science portal
GeeksforGeeks
示例5: 关键点在于当使用 let 来声明一个变量时,它会被放置在代码块的顶部,但未进行初始化操作。因此,如果在声明变量之前就使用了 let 变量,就会出现引用错误 。
HTML
<script>
x = "A variable"
let x = "GeeksforGeeks";
console.log(x);
</script>
输出:
ReferenceError: Cannot access 'x' before initialization
示例6: 在这个示例中,我们将演示如何在函数作用域内外编写 let 变量。
Javascript
let name = "GeeksforGeeks";
function displayName(){
let prefix = "Hi!"
console.log(prefix + " " + name);
}
displayName();
// This code is contributed by Aman Singla....
输出:
Hi! GeeksforGeeks
const: const也是一个关键字,用来声明块作用域的变量,但是由const关键字声明的变量在同一作用域内不能被更新。与let变量类似,const变量既不能重新声明,也不能在声明之前访问。
语法:
const x = 12;
特征:
- 通过const 关键字声明的变量在同一范围内或代码块内保持不变。
示例1:
HTML
<script>
const x = 12;
x = "GeeksforGeeks";
console.log(x);
</script>
输出:
TypeError: Assignment to constant variable.
示例2: 常量变量可以在块作用域内声明为局部变量,而在块作用域内,局部变量的优先级高于全局变量。
HTML
<script>
const x = 12; // Global variable
const y = "Welcome"; // Global variable
{
const x = "GeeksforGeeks"; // local variable
// Expected output: GeeksforGeeks
console.log(x);
// Expected output: Welcome
console.log(y);
}
// Expected output: 12
console.log(x);
</script>
输出:
GeeksforGeeks
Welcome
12
示例3: 如果我们尝试使用const 关键字声明一个对象,那么该对象不能被更新,但对象的属性仍然可以被更新。
HTML
<script>
const obj = {
name: "GeeksforGeeks",
message: "A computer science portal"
}
// Updating properties of obj
obj.message = "Welcome to GeeksforGeeks";
console.log(obj.name);
console.log(obj.message);
</script>
输出:
GeeksforGeeks
Welcome to GeeksforGeeks
示例4: 类似于 let
,const
对象在声明之前必须被初始化。如果未初始化,将会抛出一个引用错误 Reference error
。
HTML
<script>
x = "Welcome to GeeksforGeeks"
const x = 12;
console.log(x);
</script>
输出:
ReferenceError: Cannot access 'x' before initialization
示例5: 在这个示例中,我们将展示如何在函数作用域内部和外部使用使用 const 关键字声明的变量。
Javascript
const name = "GeeksforGeeks";
function displayName(){
const prefix = "Hi!"
console.log(prefix + " " + name);
}
displayName();
输出:
Hi! GeeksforGeeks
关键区别: 所以我们可以看到,使用let 关键字声明的变量可以被重新赋值,而使用const 关键字声明的变量在相同的作用域内永远不能被重新赋值。