Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Es6 declares whether the variable should be initialized

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "es6 declaration variables should not be initialized". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "es6 declaration variables should not be initialized" can help you solve the problem.

ES6 declares variables that do not have to be initialized. In es6, variables declared with the keywords var and let can be modified, so they can not be initialized. At this time, variables will be given an initial value of "undefined" by default; variables declared with the keyword const cannot be modified, they must be initialized, otherwise an error will be reported.

Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ES5 has only two ways to declare variables: var and function, while ES6 adds let and const commands.

Among them, variables are declared using the keywords var and let, which can be uninitialized.

var a ; //undefinedlet b; //undefined

The variables defined by var and let can be modified. If they are not initialized, they will be given an initial value of "undefined" by default; undefined will be output without error.

To declare variables with the keyword const, it must be initialized.

The variable declared by the word const is a constant, which must be initialized when defined, and the value cannot be modified after initialization.

Grammar:

const variable name = value;const variable name 1= value 1, variable name 2= value 3,..., variable name n= value n;

Note: Constant and variable are used to store data containers, but the value of the constant can not be changed during the operation of the program, otherwise it will report an error during operation.

Examples:

const a = 1;const b; //error, must be initialized

This variable is either a global variable or a global variable within a module.

If a variable is assigned only once when it is declared and never reassigned in other lines of code, const should be used, but the initial value of the variable may be adjusted in the future (constant variable).

Create a read-only constant that appears unmodifiable in different browsers; it is recommended not to modify it after declaration; it has block-level scope

const represents a constant index of a value, that is, the pointer to the variable name in memory cannot change, but the value pointing to the variable may change.

const The variables defined by const cannot be modified. Generally, when requiring a module, use or define some global constants.

Constants can be declared globally or within functions, but they must be initialized

A constant cannot have the same name as any other variable or function in its scope.

On the "es6 declaration variables do not want to initialize" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report