Home  >  Article  >  Web Front-end  >  What are the naming rules for javascript variables

What are the naming rules for javascript variables

青灯夜游
青灯夜游Original
2021-06-15 12:10:515181browse

Naming rules for JavaScript variables: 1. Variable names can contain letters, numbers, dollar signs, or underscores, but they must start with letters, dollar signs, or underscores, not numbers. 2. Keywords or reserved words cannot be used to name variables. 3. The variable name must be meaningful and be able to describe the type of information stored in the variable.

What are the naming rules for javascript variables

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The data in the program is handed over to the CPU for processing, and our general operation is to store this data before handing it over to the CPU for processing. So how do we store the data in the program? This Just use variables.

What are variables?

Variables are used to store data in programs. Variables can store data in them. We We can give this variable an easy-to-remember alias. When we want to use this data, we can find this variable through this alias and use the data stored in it.

1), variable declaration

The syntax for creating a variable is like this:

let 变量名; //声明变量的语法
let name; //声明1个变量,变量的名字叫做name
let age; //声明1个变量,变量的名字叫做age

2), assignment symbol

After the variable is created, data can be stored in the variable using the assignment symbol.

变量名 = 数据;
name = "jack"; //将字符串 "jack" 这个数据存储到 变量 name 中
age = 18; //将数值类型的数据 18 这个数据存储到 变量 age 中

Assignment symbol = , its function is to store the data on the right into the variable on the left.

3) Use of variables

Where you need to use the data stored in a variable, you only need to write the name of the variable directly. In particular, the data stored in the variable is also called the value of the variable.

console.log(name);    //想要输出name变量中的值,直接写上变量的名字name
console.log(age);    //想要输出age变量中的值,直接写上变量的名字age

2. Naming rules and specifications of variables

1). Naming rules of variables in JavaScript

Naming rules must be followed, otherwise the code will report an error and cannot be executed

a. Variable names can contain letters, numbers, dollar signs ($) or underscores (_), but must start with letters or dollars. It starts with a symbol ($) or an underscore (_) and cannot start with a number.

Note: Hyphens (-) or periods (.) cannot be used in variable names.

b. Keywords or reserved words cannot be used to name variables.

2), naming convention

If you don’t follow it, you will not get an error and can execute it, but front-end programmers all over the world are complying with it.

a. The variable name should be meaningful. When you see the variable name, you will know what kind of data is stored in it.

b. It is best to use English words. If pinyin is not available, you can do it.

c. Use camel case nomenclature. One word is all lowercase, and the first letter of multiple words is capitalized starting from the second word.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of What are the naming rules for javascript variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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