Javascript Operators
Operators are used to perform operations. The operation can be mathematics operation, relational operation, logical operation etc. The operation can be performed on number, string, boolean, object datatypes.
Unary Operator
Unary Operator takes only one value.
Operator | Name | Example |
++ | Increment | a++ // post increment ++a // pre increment |
-- | Decrement | a-- // post decrement --a // pre decrement |
! | Logical Negation | !a // True, if it evaluates to False. |
The logical negation operator always returns boolean value, based on the operand value. It returns true if the operand is either a 0 number, empty string , null , NaN , undefined.
Example
<script>
var a1= 10;
document.write(a1++); //output: 10
document.write('<br/>');
document.write(++a1); //output: 12
document.write('<br/>');
document.write(a1--); //output: 12
document.write('<br/>');
document.write(--a1); //output: 10
document.write('<br/>');
// Logical Negation Examples
var a1 = '';
var a2 = 'Hello';
var a3 = 0;
var a4;
document.write(!a1); //output: true
document.write('<br/>');
document.write(!a2); //output: false
document.write('<br/>');
document.write(!a3); //output: true
document.write('<br/>');
document.write(!a3); //output: true
</script>
Logical Operator
These operators are used to perform logical operations. It returns always a boolean value.
Operator | Name | Example |
&& | AND | var1 && var2; // Logical AND between var1 and var2 |
|| | OR | var1 || var2; // Logical OR between var1 and var2 |
^ | XOR | var1^var2; |
! | NOT | !(var1 && var2); |
Arithmetic Operator
Arithmetic operators work on only two numeric operands, if the operands are not numeric then it automatically converts to numeric
Operator | Name | Example |
+ | Addition | a+b |
- | Subtraction | a-b |
* | Multiplication | a*b |
/ | Division | a/b |
% | Modulus | a%b |
Example
<script>
var a = 10;
var b = 12;
var c = a + b;
var d = a - b;
var e = a * b;
var f = a / b;
var g = a % b;
document.write(c + '<br/>');
document.write(d + '<br/>');
document.write(e + '<br/>');
document.write(f + '<br/>');
document.write(g + '<br/>');
</script>
Comparison Operators
Comparison operators are used for comparison. It always returns a boolean value.
Operator | Name | Example |
== | Equal to | a == b; // check the equality between both a and b. |
=== | Identical to | a === b; // if value and datatype of both operands are equal then it returns true. |
!= | Not equal to | a != b; //If the value of both operands are not equal, then it returns true, otherwise it returns false. |
< | Less than | $a < $b; //If the value of left operand is less than the right operand than it returns true. |
> | Greater than | a > b; //If the value of left operand is greater than the right operand than it returns true. |
>= | Greater than equal to | a >= b; //If the value of left operand is greater than or equal to the right operand than it returns true. |
<= | Less than equal to | a <= b; //If the value of left operand is less than or equal to the right operand than it returns true. |
Assignment Operators
Assignment operators are used to assign a value to a variable.
Operator | Example | Description |
= | a = 2; | It assigns the right operand value to the left operand. |
+= | a += 2; // Same as a = a+2; | It adds both operand and assign to the first operand. |
-= | a -= 2; // Same as a = a-2; | It adds both operands and assign the result to the first operand. |
*= | a *= 2; // Same as a = a*2; | It multiplies both operands and assign the result to the first operand. |
/= | a /= 2; // Same as a = a/2; | It divides left operand to the right and assign the result to the first operand. |
%= | a %= 2; // Same as a = a%2; | It takes modules and assign the result to the first operand. |
Example
<script>
var a = 10;
document.write(a += 2); // Output : 12
document.write('<br/>');
document.write(a -= 2); // Output : 10
document.write('<br/>');
document.write(a *= 2); // Output : 20
document.write('<br/>');
document.write(a /= 2); // Output : 10
document.write('<br/>');
document.write(a %= 2); // Output : 0
</script>