What is a variable?
A variable is a container used to temporarily store values. In a program, the value stored in the variable can be changed.
Variable Declaration (Declaring Variables)
Declare a variable before using it. In Javascript, use var to declare variables.
Variables can be declared using the following methods:
1. Declare a variable at a time. Example:
Var;
2. Multiple variables are declared at the same time, and variables are separated by commas. Example:
Var a, B, c;
3. When declaring a variable, assign the initial value to the variable. Example:
Var a = 2;
4. Declare multiple variables at the same time and assign them the initial values. Variables are separated by commas. Example:
Var a = 2, B = 5;
Variable naming rules
Variable names can be of any length. The variable name must comply with the following rules:
The first character of the variable name must be an English letter or an underscore (underscore )_
The first letter of the variable name cannot be a number. It can contain English letters, numbers, and underlines (underscore )_
Arithmetic operators
Example of operator instructions
+ Add x + y if x is an integer of 2, y is an integer of 5, and x + y is equal to 7
If x is the string "text1" and y is the string "fun ",
X + y equals to "text1fun"
-Subtraction x-y
* Multiplication x * y
/Division x/y
% Division of the two returns the remainder x % y. If x is equal to 10, y is equal to 3, and x % y is equal to 1.
++ Increments x ++. If x is equal to 10, x ++ is equal to 11.
-- Decrease y -- if y equals 10, y -- equals 9
Logical operators
Example of operator instructions
= Equal to x = y if x is equal to 2 and y is equal to 2, x = y
=== All equals (values are equal, data types are also equal) x === y if x is equal to integer 2, y is string "2 ",
Then x = y is not true.
> Greater than x> y
>=Greater than or equal to x> = y
<Less than x <y
<= Less than or equal to x <= y
! = Not equal to x! = Y
! = Not all equal to x! = Y
& And (and) x <10 & y> 1
! Non (not )! (X = y)
| Or (or) x = 8 | y = 8
Value assignment operator
Example of operator instructions
= Assign value x = 5 assign the value of integer 5 to variable x
Note: pay attention to the difference between value assignment (=) and equal to (=.