Javascript Operators

tutorial and example
tutorial and example

In JavaScript operators are symbol that are used to perform operations on operands. In simple words 3+2 is equal to 5. Here 3 and 2 are called operands and + is called the operator.

Following are the operators used in JavaScript:

  1. Arithmetic Operators.
  2. Comparison (relational) Operators.
  3. Bitwise Operators.
  4. Logical Operators.
  5. Assignment Operators.
  6. Special Operators.

Arithmetic Operators

In arithmetic operators we use arithmetic operations on the operands.

Operators Description
+(Addition) Add two operands Example: A+B
-(subtraction) Subtraction from one operands to another one Example: A-B
*(Multiplication) Multiply both the operands Example: A*B
/(Division) Divide the numerator by the denominator Example: B/A
%(Modulus) Remainder of an integer division is 0 Example: B%A will give 0
++(Increment) Increase an integer value by one Example: If value A is 9, then A++ will give 11
–(Decrement) decrease an integer value by one Example: If value A is 11, then A– will give 7

Comparison Operators

In Comparison Operator compare two operands A and B.

Operators Description
==(Equal) It checks the value of two operands is equal or not, if yes then the condition becomes true. E.g.- A=1, B=2. (A==B) condition is not true
!=(Not Equal) It checks the value of two operands is equal or not, if the values are not equal, then the condition becomes true. E.g.- A=1, B=2. (A!=B) is true.
>(Greater than) It checks the value between two operands which one is greater, if condition is right then, it display true. E.g.- (A>B) is not true.
<(Less than) It checks the value between two operands which one is less, if condition is right then, it display true. E.g.- (A<B) is true.
>=(Greater than or Equal to) It checks the value of the first operand is greater than or equal to the value of second operand, if condition is right then it displays true. E.g. – (A>=B) not true.

Assignment operator

JavaScript support following assign operators.

Operator Description
=(Simple Assignment) It is used to assigns a value to a variable. E.g.- var x = 10;
+=(Add and Assignment) It is used to add the value of right operand to a variable and assigns the result to the variable. E.g.- Operator: x+=y Meaning: x =x + y
– =(Subtract and Assignment) It is used tosubtract the value of right operand to a variable and assigns the result to the variable. E.g.- Operator: x – = y Meaning: x = x – y
* = (Multiply and Assignment) It is used to multiply the variable by the value of the right operand and assigns the result to the variable. E.g.- Operator: x * = y Meaning: x = x * y
/ = (Divide and Assignment) It is used to divide the variable by the value of the right operand and assign the result to the variable. E.g.- Operator: x / = y Meaning: x = x / y

Bitwise Operators

JavaScript supports following bitwise operators:

Operators Description
& (Bitwise AND) It performs the AND operation on each pair of bits. E.g.- (A & B) is 2.
| (Bitwise OR) It performs the OR operation on each pair of bits. E.g. – (A | B) is 3.
^ (Bitwise XOR) It performs the OR operation on each pair of bits. E.g. – (A ^ B) is 1.
~ (Bitwise Not) It performs NOT operation on each pair of bits. E.g. – (~B) is -4.
<< (Left Shift) This operator shifts the first operand the specified no of bits to the left. E.g. – (A << 1) is 4.
>> (Right Shift) The left operand’s value is moved right by the number ofbits specified by the right operand. E.g. – (A >>1) is 1.
>>> (Right shift with zero) This operator is just like the >> (Right operator), except that the bits shifted in on the left are always zero. E.g. – (A >>>1) is 1.

Logical Operators

Logical operators are typically used with Boolean values.

Following are the logical operators-

Operator Description
&&(Logical AND) If both the operands are non-zero, then condition is true. E.g. – (A && B) is true.
|| (Logical) OR If any of the two operands are non-zero, then the condition is true. E.g. – (A || B) is true.
! (Logical NOT) Reverses the logical state of its operand. If the condition is true, then the Logical NOToperator will make it false. E.g.- ! (A && B) is false.