Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to declare variables in javascript

Shulou Source: shulou.com Published: 2022-05-31 11:46:16 09月15日 Update

Most people do not understand the knowledge points of this article "how to declare variables in javascript", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to declare variables in javascript" article.

Js declaration of variables: 1, using the var keyword declaration, syntax "var variable name;" or "var variable name = value"; 2, use the let keyword declaration, syntax "let variable name;" or "let variable name = value"; 3, use the const keyword declaration, syntax "const variable name = value;"

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

JavaScript is a weakly typed language and can be used directly without declaring variables. This is simple but not easy to find errors in variable names, so it is not recommended. It is common practice to declare the variable before using the JavaScript variable. Currently, there are three ways to declare JavaScript variables, using the var, let, and const keywords.

Among them, using var to declare variables is the way used by the ECMAScript6 version before. Because the variables declared in this way will cause some problems in some cases, two ways of declaring variables, let and const, are added in the ECMAScript6 version.

JavaScript takes the form of weak data types, so the JavaScript variable is a free variable. It can accept any type of data during the running of the program, and no matter which way it is declared, there is no need to specify the data type when declaring it, which is very different from the data type of the variable declaration in strongly typed languages such as Java.

Var, let, and const can all declare variables, but there are many differences between them, which are described below.

1. Declare variables using var

Using var, you can declare variables with global or function-level scope, and there are several ways to declare syntax.

Mode 1: var variable name; Mode 2: var variable name 1, variable name 2, … , variable name n; method 3: var variable name 1 = value 1, variable name 2 = value 2,... , variable name n = value n

1) using var, you can declare one variable at a time, or you can declare multiple variables at a time, separated by commas. For example:

Var name; / / declare one variable at a time var name,age,gender; / / declare multiple variables at a time

2) you can declare a variable without initialization (that is, assigning an initial value). At this time, the default value is undefined;, or you can initialize the variable while declaring the variable. For example:

Var name = "Zhang San"; / / initialize variables var name = "Zhang San", age=20,gender; / / initialize some variables var name = "Zhang San", age=20,gender = 'female' in one declaration; / / initialize all variables in one declaration

3) the specific data type of the variable is determined according to the data type of the assigned value, for example:

Var message = "hello"; / / value is a string type, so the type of message variable is string type var message = 123; / / value is numeric, so the type of message variable is numeric type Var message = true;// value is Boolean type, so the type of message variable is Boolean type

4) in practical applications, the declaration of loop variables is often directly regarded as part of the loop syntax. For example:

For (var iTuno Bandi)

Tags: Variables types methods data syntax content keywords keywords procedures runs differences codes formats Zhang San between functions initial values multiple characters at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Linux MariaDB Shulou Information