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

How to define variables in javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to define variables in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In javascript, you can use the var, const, or let keywords to define variables, and the syntax "keyword variable name;" or "keyword variable name = value;". Variables defined by var can be modified; variables defined by const cannot be modified and must be initialized; and let can define a local variable at the block level.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Variables are one of the foundations of all programming languages and can be used to store data, such as strings, numbers, Boolean values, arrays, and so on, and to set, update, or read the contents of variables as needed. We can think of a variable as the symbolic name of a value.

Any variable must be defined before it is used. If multiple variables are defined, different storage space is allocated for each variable.

How to define variables

In javascript, you can use the keyword: const,var,let to define variables, syntax:

Keyword variable name

Example:

Var name;var name, age, sex

Naming rules for variables

In JavaScript, variable names cannot be defined casually, and you need to follow the naming rules for identifiers, as shown below:

Variable names can contain numbers, letters, underscores _, and the dollar sign $

Chinese characters cannot appear in variable names

Variable names cannot contain spaces

Variable name cannot be a keyword or reserved word in JavaScript

The variable name cannot start with a number, that is, the first character cannot be a number.

When defining a variable, the variable name should be as meaningful as possible so that it can be easily understood by yourself or others. For example, you can use name to define a variable to store the name, and dataArr to define an array type variable.

When the variable name contains multiple English words, it is recommended to use the hump naming method (big hump: the first letter of each word is capitalized, such as FileType, DataArr; hump: the first letter of the first word is capitalized after lowercase, such as fileType, dataArr).

The difference between const, var and let

Let's take a look at the differences between the three ways to define variables in js: const, var, and let.

1. Variables defined by const cannot be modified and must be initialized.

Const b = 2 console.log / correct / / const banner / error, console.log must be initialized ('out-of-function const definition bgroup' + b); / / output value / / b = 5-console.log ('out-of-function modification of const definition b' + b); / / unable to output

2. The variable defined by var can be modified. If it is not initialized, it will output undefined and will not report an error.

Var aplash1; / / var a transposer / no error console.log ('out-of-function var definition av' + a); / / you can output a=1function change () {av 4; console.log ('intra-function var definition av' + a); / / you can output Av4} change (); console.log ('var definition an is the internal modified value of the function after a function call:' + a); / / you can output ajar 4.

3. Let is a block-level scope, and the definition of let inside the function has no effect on the outside of the function.

Let let 3; console.log ('out-of-function let definition' + c); / / output c=3function change () {let definition 6; console.log ('in-function definition let'+ c); / / output let definition c is not affected by function internal definition:'+ c); / / output let definition c is not affected by the internal definition of the function:'+ c); / / output let 3 "how to define variables in javascript" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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