4. JavaScript variables

The teaching effect of this video is limited to students who need to get started with 0 basics. The blogger will plan other courses later. If you do
n’t understand the video or have any questions, please contact me directly or I will re-record a version of the
courseware. csdn, you can also join the self-study study group, learn with peers, and communicate more conveniently
vx search official account [Front-end New Weather] If you have my WeChat, please be sure to fill in the remarks (B station - name)

Courseware code address https://github.com/haojiey/js-Introductory-courseware

what can be learned

  • Variable naming convention
  • How to declare variables
  • var method features
  • let method features
  • const method characteristics

Naming rules

Pay attention to rule
1. The variable name must start with a letter, an underscore " " or "$". Subsequent additional characters can be letters, , dollar signs, or numbers.
2. Spaces and other punctuation marks are not allowed in variable names, and the first word cannot be a number.
3. The length of the variable name cannot exceed 255 characters.
4. Variable names are case-sensitive.
5. Variable names must be placed on the same line.
6. Keywords and reserved words in js cannot be used, such as: true, false, null, if, for, switch, case, break, continue, while, var, function, etc.

Naming conventions

The variable name must be meaningful.
The variable name must be readable
. When defining a variable, make it easy for yourself or others to understand. For example, you can use name to define a variable that stores a name.
Hump nomenclature
[big hump] capitalize the first letter of each word , such as FileType [small hump] the first letter of the first word is lowercase, and the first letter of the word after it is capitalized, such as fileType)

declare variable

There are three ways to declare variables: var const let, and when a variable is created, a new space will be opened in the stack area in memory

var declaration

When defining variables, you can define one or more variables at a time
. To define multiple variables, you need to use commas between the variable names to separate
the variable definitions. If no value is assigned to the variable, then these variables will be assigned an initial value— —undefined (undefined).

//声明一个变量 a
var a;         

//申明多个变量时,变量名之间用逗号隔开
var b,c;     

document.write(a); 
//初始化值会输出undefined

variable assignment

After the variable is defined, you can use the equal sign = to assign a value to the variable. The left side of the equal sign is the name of the variable, and the right side of the equal sign is the value to be assigned to the variable, as shown in the following example:

// 定义一个变量 name
var name;
// 将变量 name 赋值为 小木
num = '小木';

num = 小木; //Uncaught ReferenceError: 小木 is not defined

// 为多个变量赋值
var b = 1,c = 2;

variable hoisting

The parsing method of the JavaScript engine is: first parse the code, get all declared variables, and then run it line by line. In this way, all declared variables will be promoted to the head of the code, which is called variable promotion (Hoisting).

document.write(a); //显示undefined
a = '小木'
document.write(a); //显示 小木
var a;

In the above example, the variable declaration is placed last, and the assignment operation is placed first. Since JavaScript has pre-parsed the variable declaration statement during pre-compilation, the first line of code will not throw an exception when reading the variable value, but return the uninitialized value undefined. The third line of code is read after the assignment operation, so it is displayed as Xiaomu.

let statement

Before 2015, JavaScript could only declare variables through the var keyword. After the release of ECMAScript6 (ES6), two new keywords, let and const, were added to declare variables.

Variables defined by let have block-level scope, and in this code block, variables with the same name cannot be declared repeatedly, otherwise an error will be reported
.

let name = "小木";      // 声明一个变量 name 并赋值为“小明”
let age  = 24;          // 声明一个变量 age
let age  = 25;          // 报错:变量 age 不能重复定义
//Identifier 'age' has already been declared
document.write(a); //ReferenceError: Cannot access 'a' before initialization
a = '小木'
document.write(a);
let a;

const

The function of the const keyword is the same as that of let, but the variable declared with the const keyword has another characteristic, that is, the
variable defined with the const keyword cannot be modified once defined, that is, the variable defined with the const keyword is a constant.
The value of const cannot be declared repeatedly in the same function, otherwise both will report an error.

const name = "小木";      // 声明一个变量 name 并赋值为“小木”
name  = '梁木由';          // 报错:变量 age 不能重复定义
// Assignment to constant variable.

const age = 24;     // 同一个变量声明两次
const age  = 25;    // Uncaught SyntaxError: Identifier 'age' has already been declared

Guess you like

Origin blog.csdn.net/weixin_55123102/article/details/130255777