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:

    type = "text/javascript" >     Set the variables to different values and different operators and then try.   

Result:

  (a == b) => false 
(a true
(a> b) => false
(a! = b) => true
(a> = b) => false
a <= b) => true
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

Logical operators

JavaScript supports the following logical operators:

Assuming variable A holds value 10 and variable B holds value 20, then:

STATE DEBT AND DESCRIPTION1

&& (AND logic)

If both operands are non-zero, then the condition becomes true.

Ex: (A && B) is true.

2

|| (Logical OR)

If 1 of the two operands is non-zero, then the condition becomes true.

Ex: (A || B) is true.

3

! (NOT logic)

Reverse the state of the operand. If a condition is true, then the OR operator will cause it to be false.

Ex:! (A && B) is false.

For example

You try the following example showing how to use logical operators in JavaScript:

    type = "text/javascript" >     
 Set the variables to different values and different operators and then try. 

Result:

  (a && b) => false 
(a || b) => true
! (a && b) => true
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

Bitwise operator (bitwise)

JavaScript supports the following bit manipulation operators:

Assuming variable A holds value 2 and variable B holds value 3, then:

STATE DEBT AND DESCRIPTION1

& (Permission AND bit)

It performs an AND logic operation on each bit of its integer parameters.

Ex: (A & B) is 2.

2

| (OR bit)

It performs a logical OR operation on each bit of its integer parameters.

Ex: (A | B) is 3.

3

^ (XOR bit)

It performs a logical exclusion OR operation on each bit of its integer parameters. Permission or exclusion means that either operand 1 is true or operand 2 is true, but not both.

Ex: (A ^ B) is 1.

4

~ (Bit negative)

It is a unary operator and reverses all bits in that operand.

Ex: (~ B) is -4.

5

<< (Left translation)

It moves all the bits in the first operand to the left with the position number specified in the second operand. The new bits are filled by zero. Shifting a value to a position is equivalent to multiplying it by 2, translating the two positions equivalent to multiplying by 4, and so on.

Ex: (A << 1) is 4.

6

>> (Right translation)

Operator must be binary. The value of the left operand is moved to the right by the number of bits specified by the right operand.

Ex: (A >> 1) is 1.

7

>>> (Right translation with Zero)

This operator is quite similar to the >> operator, except that the bits shifted to the left are always zero.

Ex: (A >>> 1) is 1.

For example

Try the following code about the Bit operation operators in JavaScript:

    type = "text/javascript" >     
 Set the variables to different values and different operators and then try. 

Result:

  (a & b) => 2 
(a | b) => 3
(a ^ b) => 1
(~ b) => -4
(a << b) => 16
(a >> b) => 0
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

Assignment operator

JavaScript supports the following assignment operators:

STATE DEBT AND DESCRIPTION1

= (Simple assignment)

Assign values ​​from the right operand to the left operand

Ex: C = A + B will assign the value of A + B to C

2

+ = (Addition and assignment)

It adds the right operand value to the left operand and assigns the result to the left operand

Ex: C + = A is equivalent to C = C + A

3

- = (Subtraction and assignment)

It subtracts the right operand from the left operand and assigns the result to the left operand

Ex: C - = A is equivalent to C = C - A

4

* = (Multiplication and assignment)

It multiplies the right operand with the left operand and assigns the result to the left operand

Ex: C * = A is equivalent to C = C * A

5

/ = (Divide and assign)

It divides the left operand for the right operand and assigns the result to the left operand

Ex: C / = A is equivalent to C = C / A

6

% = (Divide and assign division)

Divide the left operand balance for the right operand and assign the result as the remainder for the left operand

Ex: C% = A is equivalent to C = C% A

Note - Along with the logic development circuit for Bit operators, we will have the following operators: << =, >> =, >> =, & =, | = and ^ =.

For example

The following example is about using Bit operation operators in JavaScript:

    type = "text/javascript" >     
 Set the variables to different values and different operators and then try. 

Result:

  Value of a => (a = b) => 10 
Value of a => (a + = b) => 20
Value of a => (a - = b) => 10
Value of a => (a * = b) => 100
Value of a => (a / = b) => 10
Value of a => (a% = b) => 0
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

Mixed operator

Here, we will discuss two operators that are quite useful in JavaScript: Conditional operator (? :) and typeof operator .

Conditional operator (? :)

The conditional operator first calculates an expression to see if it is true or false and then executes either of the provided commands depending on the result of the calculation.

? : (Condition): If the condition is true? Then the value X: If not then the value Y.

For example

Try the following code to understand how conditional operators work in JavaScript:

    type = "text/javascript" >     
 Set the variables to different values and different operators and then try. 

Result:

  ((a> b)? 100: 200) => 200 
((a 100
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

Typeof operator

The typeof operator is a unary operator that is preceded by its single operand, which can be of any type. Its value is a string indicating the data type of the operand.

The typeof operator evaluates "number", "string", or "boolean" if its operand is numeric, string, or logical and returns either true or false on the estimate.

Below is a list of return values ​​for the typeof operator.

Type string returned by typeofNumber "number" operator String "string" Boolean "boolean" Object "object" "function" Undefined "undefined" function Null "object"

For example

The following code shows how to use the typeof operator.

    type = "text/javascript" >     
 Set the variables to different values and different operators and then try. 

Result:

  Result => B is String 
Result => A is Numeric
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .

According to Tutorialspoint

Previous article: Variable in JavaScript

Next lesson: if . else command in JavaScript

Thanks for reading the post

Operator in JavaScript

Please share this article to your friends
« If ... else command in JavaScript
Variable in JavaScript »