Javascript Operator
We are familiar with mathematical operators and operands. Now we will see how they work in Javascript. The numbers (in an arithmetic operation) are called operands. An operator defines the operation (to be performed between the two operands). In Javascript, there are some kinds of operators. Like as –
- Arithmetic Operator
- Comparison Operator
- Logical/ Relational Operator
- Assignment Operator
- Conditional/ Ternary Operator
Arithmetic Operator
Arithmetic operators are used to performing arithmetic on numbers. An arithmetic operator takes numerical values as their operands and returns a single numerical value.
Addition
Between two operands after operation addition ( + ) operator adds those two operands and returns another operand.
10 + 220
Output:
230
We can do the same thing with the assignment operator
Example:
var x = 10; // assign the value 10 to x
var y = 220; // assign the value 20 to y
var z = x + y; // assign the value 30 to z (10 + 20)
console.log(z);
Output:
230
Subtraction
Between two operands after operation subtraction ( – ) operator subtracts the second operand from the first one and returns another operand.
220 - 10
Output:
210
We can do the same thing with the assignment operator
Example:
var x = 220; // assign the value 20 to x
var y = 10; // assign the value 10 to y
var z = x - y; // assign the value 10 to z (20 - 10)
console.log(z);
Output:
210
Multiplication and Division
Between two operands after operation multiplication, the ( *, / ) operator multiplies those two operands and returns another operand. Between two operands after the operation, the division operator divides the first operand by the second one and returns another operand.
Example:
var x = 10; // assign the value 10 to x
var y = 20; // assign the value 20 to y
var p = x * y; // assign the value 200 to z (10 * 20)
var q = y / x; // assign the value 2 to z (20 / 10)
console.log(p);
console.log(q);
Output:
200
2
Modulus and Exponential
The ( % ) operator is used for finding the remainder in the division operation. Then ( ** ) operator is used for finding the exponential of a number.
Example:
var x = 14;
var y = 5;
var z = x % y;
console.log(z);
var x = 14;
var y = x**2;
console.log(y);
Output:
4
196
Increment and Decrement
The Increment ( ++ ) operator adds value 1 with the operand and the decrement ( — ) operator subtracts value 1 from the operand. This is kind of
x = x + 1;
y = y – 1;
Example:
var x = 14;
x++;
console.log(x);
var y = 5;
y--;
console.log(z)
Output:
15
4
Comparison Operators
Comparison operators are used to compare two values:
Name | Operator | Operation | Return Type |
Equal | == | x == y | True / False |
Not equal | != | x !=y | True / False |
Greater than | > | x > y | True / False |
Less than | < | x < y | True / False |
Greater than and equal | >= | x >= y | True / False |
Less than and equal | <= | x <= y | True / False |
Example:
var x = 14;
var y = 23;
if x == y ;
if x != y;
if x >= y;
if x <= y;
Output:
>>> false
>>> true
>>> false
>>> true
Logical Operators
Logical operators are used to combining conditional statements:
Details | Operator | Operation |
Returns True if both statements are true | And ( && ) | x > 42 && y < 23 |
Returns True if one of the statements is true | Or ( || ) | x < 42 || y > 23 |
Reverse the result, and returns False if the result is true | Not ( ! ) | ! ( x > 42 && y < 23 ) |
Example:
var x = 34;
var y = 21;
x > 42 && y < 23 ;
x > 42 || y < 23;
! (x > 42 && y < 23 ;)
Output:
>>> false
>>> true
>>> true
Assignment Operator
Assignment operators / Inplace operators are used to assign values to variables:
Main Task | Operator | Operated Task |
x = 29 | = | x = 29 |
x = x + 29 | += | x += 29 |
x = x – 29 | -= | x -= 29 |
x = x * 29 | *= | x *= 29 |
x = x / 29 | /= | x /= 29 |
x = x // 29 | //= | x //= 29 |
x = x % 29 | %= | x %= 29 |
x = x ** 29 | **= | x **= 29 |
x = x & 29 | &= [‘&’ Bitwise AND] | x &= 29 |
x = x | 29 | |= [‘|’ Bitwise OR] | x |= 29 |
x = x ^ 29 | ^= [‘^’ Bitwise XOR] | x ^= 29 |
x = x >> 29 | >>= [‘>>’ Right Shift ] | x >>= 29 |
x = x << 29 | <<= [‘<<’ Left Shift] | x <<= 29 |
Example:
var x = 34;
x += 29;
x -= 29;
x *= 29;
x %= 29;
Output:
>>> x == 63
>>> x == 5
>>> x == 986
>>> x == 5
Conditional / Ternary Operator
The ternary operator is a simplified conditional operator like if / else.
Syntax: condition? <expression if true> : <expression if false>
Here is an example:
var x = 10;
var y = 20;
var c = a > b ? 100 : 200;
Here assignment of variable c depends on the condition of whether a is greater than b or not. If the condition satisfies, yes then c will assign 100 otherwise 200.
Output:
>>> c = 200;