JavaScript Basics 05 - Introduction to literals, variables and basic use of variables

Hello everyone, I am Lei Gong!
When talking about variables, I feel very familiar, but when I want to explain what a variable is, I feel a little bit speechless. Just like explaining why 1+1=2, I feel very familiar. I know it, but I can't explain it.
However, although I am familiar with it in other scenarios, there are definitely differences in JavaScript. I will also follow the tutorial and learn it to lay a solid foundation. Below are study notes.

1. Literal value

Literal value is like what we often say. In computers, literal value refers to the quantity that describes something or something, and is a fixed value.

2. What are variables?

Literally means a constantly changing quantity. Relative to literals.
◆ In vernacular: A variable can be understood as a box that holds things.
◆ Written language: In computer programs, variables are containers used to save data and store information. Just like the registers in PLC that use classes to store data.
Insert image description here

Warm reminder : A variable is not the data itself, it is a container used to store data. It can be understood as a jar used to hold things.

3. Application of variables

1. Define (declare) JS variables
In JS, before using variables, you need to create them first, which can also be said to declare/define variables.
grammar:

let 自定义变量名;

Declaring variables consists of two parts: declaration keywords and custom variable names;
let is the keyword, which is the word provided by the system for declaring variables.

2. Assign a
value to the variable. After declaring the variable, you need to initialize the variable. That is, assign an initial value to the variable. Add "=" after the variable name, followed by the value to be assigned.
This is consistent with the syntax in many other languages, such as C#.
An example is shown below:
Insert image description here

Note : In the above picture, alert(age) obtains the data inside through the variable name.
Similar to alert(age) in the program, there is no need to add quotes when outputting variables in the program. If quotes are added to the variable, the output result will be the name of the variable itself.
3. Things to note when applying variables

3.1. Which custom variable names are not standardized?
3.1.1. Variable names cannot start with numbers;
3.1.2. There cannot be spaces in the middle of variable names;
3.1.3. Variable names can use special symbols, but only "_".
3.1.4. It is not recommended to use Chinese characters in variable names;
3.1.5. Keywords cannot be used in variable names;

3.2. Execution order of assignment operator (=)
When encountering an assignment operator, the code on the right side of the assignment operator is executed first, and then the left side is executed.

3.3. Update variables.
Variables can only save one value at a time. If you assign a value to the assigned variable again, the last assignment will be updated, and only the last assignment result will be retained.
Example:
Insert image description here

3.4. The definition and assignment of variables can be written directly
. Example:

let age=18

3.5. Declare multiple variables.
If a keyword defines multiple variables, they can be set directly. Use commas (,) to separate multiple variables.
Example:

let a =666 ,b= 888; c=999;

Note : Although writing this way reduces the total number of lines of code and looks shorter, it is not recommended in practice. For better readability, generally only declare one variable per line.
3.6. When using variables, they must be defined first and then used.

4. Postscript

The above is a self-study of the basic knowledge of JavaScript literals and variables. Please correct me if there are any errors recorded.

[Quotes are powerful]
Life is like a fable, its value does not lie in its length, but in its content. 11Seneca

Guess you like

Origin blog.csdn.net/u013097500/article/details/132652410