JavaScript Operator

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.

Types of Operator in JavaScript

  1.     Arithmetic Operator
  2.     Comparison Operator
  3.     Logical Operator
  4.     Assignment Operator
  5.     Conditional Operator
  6.     Ternary Operator

Arithmetic Operator:- An arithmetic operator is a mathematical function that takes two operands and performs a calculation on them. In arithmetic operator we have two types of operator 1. binary operator, which apply on two operands. 2. Unary operator, which apply on single operand.

 

Operators

Type Of Operator Description
+ Binary Operator Addition Operator, Add two numeric operands.String Concatenation.
Binary Operator Subtraction Operator, Subtract right operand from left
* Binary Operator Multiplication Operator, Multiply two numeric operands
/ Binary Operator Division Operator, Divide left operand by right
% Binary Operator Modulus Operator, Return remainder of two operand after division of two operands
++ Unary Operator Increment Operator, Increase operand value by one  
Unary Operator Decrement Operator, decrease operand value by one  

Binary Operator:- The operator which operates on two operands and manipulates them to return a result. Following is the example of binary operators.

var x = 3, y = 6; 

var z = x + y; //performs addition and returns 9 

z = y - x; //performs subtraction and returns 3 

z = x * y; //perform multiplication and returns 18 

z = y / x; //perform division and returns 2 

z = y % 2; //returns division remainder 0 

z = x % 2; //returns division remainder 1

Unary Operator:- The operator which apply on single operand and manipulate its value is known as unary operator.

var x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here

String Concatenation:- The + operator performs concatenation operation when one of the operands is of string type.The following example show string concatenation even if one of the operands is a string.

var a = "Hello ", b = "World" , c = 5;

a + b; //returns "Hello World"

a + true; //returns "Hello true"

a + c; //returns "Hello5"

Comparison Operator:- Comparison operators are used in conditional expressions to determine which block of code executes, thus controlling the program flow. Comparison operators compare two values in an expression that resolves to a value of true or false.

Operator Description
< Returns true if right-side value is greater than left-side value; otherwise returns false.
> Returns true if left-side value is greater than right-side value; otherwise returns false.
<= Returns true if right-side value is greater than or equal to the left-side value; otherwise returns false.
>= Returns true if left-side value is greater than or equal to the right-side value; otherwise returns false.
!= Returns true if right-side value is not equal to the left-side value; otherwise returns false.
== Returns true if right-side value is equal to the left-side value; otherwise returns false.
=== Compare type of two operands.

Example:-The following example demonstrates the comparison operators.

var a = 5, b = 10, c = "5";

a > b; // returns false

a < b; // returns true

a >= b; // returns false

a <= b; // returns true

a == c; // returns true

a === c; // returns false, because 'a' is integer and 'c' is string.

var x = b;

b == x; // returns true

a != b; // returns true

Logical Operator:-  The operator that performs a logical conjunction on two statements and returns TRUE or FALSE value.Following is list of logical operators.

Operator Description
||(OR) This is binary operator means need two operands to apply. It returns 1 if any one of them is non-zero; otherwise, returns 0.
&&(AND) This is binary operator means need two operands to apply.It returns 1 if both of them is non-zero; otherwise, returns 0.
!(NOT) It reverse the outcome of the expression.!(true) returns false and !(false) returns true.

Example:-The following example demonstrates the logical operators.

var a = 5, b = 10;

(a > b) || (a == b); // returns false, becauseboth the expression are false.

(a < b) || (a == b); // returns true,second expression is false but first expression is true.

(a != b) && (a < b); // returns true, beause both the expression are true.

!(a < b); // a is less than b which is true and !(true)returns false.

!(a > b); // a is not greater than b wich is false and !(false) returns true.

Assignment Operator:- The assignment operators are used to assign values to variables.

Operator Description
= Assigns right operand value to the left operand.
+= Sums up left and right operand values and assigns the result to the left operand.
-= Subtract right operand value from the left operand value and assigns the result to the left operand.
*= Multiply left and right operand values and assigns the result to the left operand.
/= Divide left operand value by right operand value and assign the result to the left operand.
%= Get the modulus of left operand divide by right operand and assign resulted modulus to the left operand.

Example:-The following example demonstrates the assignment operators.

var x = 5, y = 10;

x = y; //x would be 10

x += 1; //x would be 11

x -= 1; //x would be 10

x *= 5; //x would be 50

x /= 5; //x would be 10

x %= 2; //x would be 0

Ternary Operator:- Ternary operator ?: that assigns a value to a variable based on some condition. This is the short form of the if-else statement.

Operator Description
?: <condition> ? <value1> : <value2>

if the condition true then first value apply otherwise second.

Example:-The following example demonstrates the ternary operators.

var a = 10, b = 5;

var c = a > b? a : b; // value of c would be 10

var d = a > b? b : a; // value of d would be 5

 

Back                                                                                                                                               Next