Operator in JavaScript
We see the following simple expression: 4 + 5 is equal to 9. Here 4 and 5 are operands and '+' called operator - operator.
What is an operator?
We see the following simple expression: 4 + 5 is equal to 9. Here 4 and 5 are operands and '+' called operator - operator. JavaScript supports the following operator types:
- Arithmetic operator
- Comparison operator
- Logical operators (or relations)
- Assignment operator
- Conditional operator
We now consider each operator one.
Arithmetic operator
JavaScript supports the following arithmetic operators:
Assuming variable A holds value 10 and variable B holds value 20, then:
STATE DEBT AND DESCRIPTION1+ (Addition)
Add two operands
Ex: A + B will result in 30
2- (Subtraction)
Subtract the second operand from the first operand.
Ex: A - B will result in -10
3* (Multiplication)
Multiply two operands
Ex: A * B will result in 200
4/ (Division)
Divide the divisor by the divided number
Ex: B / A will result in 2
5% (Split division)
The result is the remainder of the division.
Ex: B% A will result in 0
6++ (Increase to 1)
Increase integer value to 1
Ex: A ++ will result in 11
7- (Reduced 1)
Reduce an integer value to one
Ex: A-- will result in 9
Note - The plus (+) operand works with numbers as well as strings, for example: "a" + 10 will result in "a10".
For example
The following code shows how to use arithmetic operators in JavaScript:
Set the variables to different values and then try.
Result:
a + b = 43
a - b = 23
a / b = 3.3
a% b = 3
a + b + c = 43Test
a ++ = 33
b-- = 10
Đặt biến vào các giá trị khác nhau và thử thử .
Comparison operator
JavaScript supports the following comparison operators:
Suppose variable A holds value 10 and variable B holds a value of 20, then:
STATE DEBT AND DESCRIPTION1= = (Equal)
Check if the value of the two operands is equal or not, if any, the condition becomes true.
Ex: (A == B) is not true.
2! = (Unbalanced)
Check if the value of the two operands is balanced or not, otherwise the condition becomes true.
Ex: (A! = B) is true.
3> (Larger)
Check if the value of the left operand is greater than the right operand, if any, the condition becomes true.
Ex: (A> B) is not true.
4<(Smaller)
Check if the value of the left operand is less than the right operand, if any, the condition becomes true.
Ex: (A 5
> = (Greater than or equal to)
Check if the value of the left operand is greater than or equal to the right operand, if any, the condition becomes true.
Ex: (A> = B) is not true.
6<= (Less than or equal to)
Check if the value of the left operand is less than or equal to the right operand, if any, the condition becomes true.
Ex: (A <= B) is true.
For example
The following code shows how to use comparison operators in JavaScript: