Introduction to...LOGIN

Introduction to the concept of JavaScript variables

The concept of variable

  • A variable is a quantity that changes.

  • A variable can be seen as a container that holds data

  • A variable can be seen as an "unknown number". x = 10

  • The variable can be regarded as a "symbol" and "code".

  • Variables generally refer to program data.

  • Variables exist and operate in memory.

  • Variables are temporary data. We can think of computer memory as "small grids" one by one. Each "small grid" can store the name of a variable and the value of the variable.


Declaration of variables

  • The declaration of variables is equivalent to booking a "room" in a hotel.

  • Syntax format: var variable name = variable value

  • Variables are declared using the system keyword var.

  • Example:

  • var name; //Declare a variable

  • var name,like,age; //Declare multiple variables at the same time, separated by commas in English

  • var name = "Xiao Ming"; //On one side Declaration side assignment


Naming rules for variables

  • Variables The name can contain letters, numbers, and underscores.

  • Variable names cannot start with numbers, but can start with letters or underscores. Such as: var _name; (correct) var 3abc; (wrong)

  • The variable name cannot be a system keyword. Such as: var, switch, for, try, case, else, while, etc.

  • Variable names in JS are case-sensitive. For example: name and Name are two variables

  • The names of variables in JS must be meaningful.

  • If the variable name consists of multiple words, how should it be expressed?

  • "Camel case naming": the first word is all lowercase, and the first letter of each subsequent word is capitalized. For example: var getUserName

  • "Underline naming": all words are lowercase and connected with underscores in the middle. For example: var get_user_name


Assign a value to the variable

  • Variable assignment is to put things into "space".

  • Use the assignment sign "=" to assign a value to a variable.

Syntax: var variable name = variable value

Example: var name = "Xiao Ming";

Note: "=" understand

  • Assign the "operation result" on the right side of "=" to the variable name on the left.

  • The right side of "=" should be operated first, and then the result of the operation is assigned to the variable on the left.

  • The left side of "=" can only be a variable name, not an "operational expression".

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>php.cn</title>
        <script>
            //声明变量x,然后把8赋值给x
            var x = 8;
            //声明变量y,然后把x+80的结果赋值给y
            var y = x+80;
            //分别输出x,y
            document.write(x);
            document.write(y);
        </script>
    </head>
    <body>
    </body>
</html>

Note:

  • Every statement in JS usually ends with a semicolon (;) in English. This semicolon is not required. In order to be compatible with PHP, it is best to add a semicolon.

  • Operators, variables, and operations can be separated by spaces, making such a program easier to read.

  • var a = 100 and var a=100 are the same


Next Section
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <script> //声明变量x,然后把8赋值给x var x = 8; //声明变量y,然后把x+80的结果赋值给y var y = x+80; //分别输出x,y document.write(x); document.write(y); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware