Variables of JavaScript Foundation

  • 2021-12-05 05:31:44
  • OfStack

Directory 1. Variable Overview 1.1 Variable in Memory Storage 1.2 Variable Use 1. Declare Variable 2. Assignment 3. Variable Initialization 1.3 Variable Syntax Extension 1. Update Variable 2. Declare Multiple Variables 3. Declare Variable Special Case 1.5 Variable Naming Convention Summary

1. Overview of variables

1.1 Storage of variables in memory

Essence: A variable is a piece of space applied by a program in memory to store data

1.2 Use of variables

The use of variables is divided into two steps: 1. Declare variables 2. Assign values

1. Declare variables

// Declare variables 
var age; // Declaration 1 The name is age Variables of 

var is an JS keyword, used to declare variables (variable variable meaning), using this keyword to declare variables, the computer will automatically allocate memory space for variables age is the defined variable name, we want to access the allocated space in memory through the variable name

2. Assignment

age = 10; // To age Variable is assigned to 10

3. Initialization of variables

var age = 10;

1.3 Variable syntax extension

1. Update variables

After a variable is re-assigned, its original value will be overwritten, and the value of the variable will be subject to the last assigned value.

2. Declare multiple variables

When declaring multiple variables at the same time, you only need to write one var, and the names of multiple variables are separated by English commas


var age = 10,name = 'lili',sex = 2;

3. Declare variable exceptions
情况 说明 结果

var age;

console.log(age);

只声明不赋值 undefined
console.log(age); 不声明 不赋值 直接使用 报错

age = 10;

console.log(age);

不声明 直接使用 10

1.5 Variable naming convention

It consists of letters (A-Z a-z), numbers (0-9), underscores (_), and dollar signs ($), such as usrAge, num01, _ name Strictly case sensitive, var APP; And var app; Are two variables You can't start with a number It cannot be a keyword or reserved word, such as for, var, while Variable name must have meaning Obey the hump nomenclature. The first letter is lowercase, and the first letter of the following words should be capitalized. myFirstName Recommended translation website: Youdao ICIBA

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: