[JavaScript tutorial] JavaScript Operators
JavaScript Operators
Operator = is used to assign values.
Operator + is used for value adding.
Operator = is used to assign values to JavaScript variables.
Arithmetic Operator + is used to add up values.
Instance
Specify the variable value and add the value together:
y=5;z=2;x=y+z;
After the preceding statement is executed, the value of x is:
7
JavaScript Arithmetic Operators
Operator
Description
Example
X operation result
Y operation result
Online instance
+ Addition x = y + 2 7 5 instances»
-Subtraction x = y-2 3 5 instances»
* Multiplication x = y * 2 10 5 instances»
/Division x = y/2 2.5 5 instances»
% Modulo (remainder) x = y % 2 1 5 instance»
++ Auto-increment x = ++ y 6 6 instances»
X = y ++ 5 6 instances»
-- Auto-Subtract x = -- y 4 4 instance»
X = y -- 5 4 instances»
JavaScript assignment operator
The value assignment operator is used to assign values to JavaScript variables.
Given x = 10 and y = 5, the following table describes the assignment operators:
Operator
Example
Equivalent
Calculation Result
Online instance
= X = y x = 5 instances»
+ = X + = y x = x + y x = 15 instances»
-= X-= y x = x-y x = 5 instances»
* = X * = y x = x * y x = 50 instances»
/= X/= y x = x/y x = 2 instance»
% = X % = y x = x % y x = 0 instance»
+ Operator used for strings
+ Operators are used to add up (join) text values or string variables ).
To connect two or more string variables, use the + operator.
Instance
To connect two or more string variables, use the + operator:
txt1="What a very";txt2="nice day";txt3=txt1+txt2;
The calculation result of txt3 is as follows:
What a verynice day
To add spaces between two strings, you need to insert spaces into a string:
Instance
txt1="What a very ";txt2="nice day";txt3=txt1+txt2;
After the preceding statement is executed, the txt3 variable contains the following values:
What a very nice day
Or insert spaces into the expression ::
Instance
txt1="What a very";txt2="nice day";txt3=txt1+" "+txt2;
After the preceding statement is executed, the txt3 variable contains the following values:
What a very nice day
Addition of strings and numbers
The sum of the two numbers is returned. If the number and string are added, the string is returned, as shown in the following example:
Instance
x=5+5;y="5"+5;z="Hello"+5;
The output results of x, y, and z are:
1055Hello5
Rule: if you add a number to a string, the result is a string!
The above is the content of the [JavaScript tutorial] JavaScript operator. For more information, see the PHP Chinese website (www.php1.cn )!