Home > Web Front-end > JS Tutorial > Scope of var, let, const

Scope of var, let, const

Linda Hamilton
Release: 2025-01-22 02:30:14
Original
232 people have browsed it

Scope of var, let, const

Detailed explanation of JavaScript variable scope

The scope of a variable refers to the area in the program where the variable can be accessed. Understanding variable scope is crucial to writing efficient, error-free code.

There are three types of variable scopes in JavaScript:

  • var
  • let
  • const

var Keywords

  • var Keywords were first introduced in 1995 with the initial release of JavaScript.
  • A variable declared using var has global scope, which means that any part of the program can access the variable.
<code class="language-javascript">var x = 10;
function inner() {
    console.log(x); // 输出:10
}
console.log(x); // 输出:10</code>
Copy after login
  • Variables declared using var will be promoted to the top of the file. This means you can try to access the variable before it's initialized, but the output will be undefined because we haven't assigned a value to the variable yet.
<code class="language-javascript">console.log(x); // 输出:undefined
var x = 10;
console.log(x); // 输出:10</code>
Copy after login

let Keywords

    The
  • let keyword was introduced in 2015 with the introduction of ECMAScript 6 and provides the functionality of block-level scoping.
  • Variables initialized with let have block scope. Therefore, the variable cannot be accessed in other scopes such as functions or the global scope.
  • Unlike var, accessing a variable declared with let before declaring it will throw an error due to the Temporal Dead Zone (TDZ).
<code class="language-javascript">console.log(x); // 输出:引发错误
let x = 10;</code>
Copy after login
  • Similarly, you cannot access block-level variables inside a function.
<code class="language-javascript">let x = 10;
function inner() {
    console.log(x); // 输出:引发错误
}</code>
Copy after login

const Keywords

  • This is another block-scoped variable. Additionally, variables declared using const must be initialized on the same line.
<code class="language-javascript">const PI = 3.14;
console.log(PI); // 输出:3.14</code>
Copy after login
  • This keyword was introduced in ECMAScript 6 and is used to store constant values ​​such as environment variables, access tokens, etc. Mainly used to store any constant value that will not change in the future.
  • If you try to change the value of a variable declared with const, a TypeError: Assignment to constant variable error will be thrown at runtime.
<code class="language-javascript">const PI = 3.14;
PI = 3.5; // 输出:TypeError: Assignment to constant variable.</code>
Copy after login

Happy programming!

The above is the detailed content of Scope of var, let, const. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template