The difference between var let const in js

下次还敢
Release: 2024-05-01 07:54:19
Original
949 people have browsed it

var, let, and const are JavaScript keywords for declaring variables. Their scope, temporary dead zone, redeclaration, block-level scope, time complexity, and purpose vary. var is declared in the global or function scope, there is no temporary dead zone, repeated declarations are allowed, and the speed is slow; let is declared in the block level scope, there is a temporary dead zone, reassignment is allowed, and the speed is fast; const is also at the block level Scope declaration is a constant, does not allow reassignment, and is faster.

The difference between var let const in js

The difference between var, let and const in JavaScript

var, let and constare keywords for declaring variables in JavaScript, and there are key differences between them.

1. Scope

  • #var:Declared in the global scope or function scope, variables in the global scope can Accessible from anywhere; variables within a function scope are only valid within the function.
  • let:is declared in a block-level scope. The block-level scope can be a code block, a function or a loop. Variables declared by let are only valid within the block-level scope.
  • const:is also declared in the block-level scope. Variables declared by const are constants and cannot be reassigned.

2. Temporary dead zone (TDZ)

  • var:There is no temporary dead zone, and the variable is Can be accessed before declaration.
  • let, const:There is a temporary dead zone, the variable can only be accessed after it is declared, otherwise an error will be reported.

3. Re-declare

  • var:can be declared repeatedly, but only one variable with the same name can exist.
  • let, const:cannot be declared repeatedly, an error will be reported.

4. Block-level scope

  • var:Block-level scope is not supported.
  • let, const:Supports block-level scope, which can effectively prevent variable conflicts.

5. Time complexity

  • var:The search speed is slower because the entire scope needs to be traversed.
  • let, const:The search speed is faster because it only needs to be searched in the current block-level scope.

6. Purpose

  • var:Suitable for variables that need to be declared in the global scope or function scope .
  • let:Applies to variables that need to be declared in block-level scope and allows reassignment.
  • const:Applicable to variables that need to be declared as constants, that is, reassignment is not allowed.

Summary

  • var:Global/function scope, allows repeated declarations, slow speed.
  • let:Block-level scope, allowing reassignment, fast.
  • const:Block-level scope, constant, no reassignment allowed, fast.

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

Related labels:
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!