What is Binary Operator in JavaScript?
Binary operator is that operator which works on two operand.
Types of Binary Operators
There are three types of binary operators:
- Arithmetical Operators (+,-,/,*,%)
- Relational Operators (>,<,>=,<=,==,!=)
- Logical Operators (&& means and,|| means or,! means not)
Arithmetic Operators
Arithmetic operators are those operators which are used to perform some mathematical calculations.
Example:
In the following table the value of num1 is 10 and num2 is 5.
OPERATOR | DESCRIPTION | EXAMPLE |
+ | Adds operands |
15=num1 +num2 |
- | Subtracts second operand from the first |
5=num1-num2 |
/ | Divides numerator by denominator.It returns quotient |
2=num1/num2 |
* | Multiplies both operands |
50=num1*num2 |
% | Modulus Operator returns the remainder after division.; |
0=num1%num2 |
Relational Operators
Relational operators are also known as comparison operators. They are used to find the relationship between two values or compare relationship between two values; on comparison they return true if the relationship is true and false if the relationship is false.
Example:
In the following table the value of num1 is 10 and num2 is 5.
OPERATOR | NAME OF OPERATOR | EXAMPLE |
> | Greater Than |
(num1>num2) is true |
< | Less Than |
(num1<num2) is false |
>= | Greater Than or Equal to |
(num1>=num2) is true |
<= | Less Than or Equal to |
(num1<=num2) is false |
== | Equal to Equal to |
(num1==num2) is false |
!= | Not Equal to |
(num1!=num2) is true |
Logical Operators
Logical operators in JavaScript are operators that returns true or false a Boolean result.
OPERATOR | NAME OF OPERATOR | EXAMPLE |
&& | AND It returns true when both conditions are true |
num1=10 and num2=20 (num1<15 && num2>30) return true |
|| | OR It returns true when at-least one of the condition is true |
num1=10 and num2=20 (num1==5 || num2<10) return false |
! | NOT(num1=10 and num2=10) It reverse the state of operand. e.g. if num1==num2 is true, logical NOT operator makes it false. |
num1=10 and num2=20 !(num1==num2) return true. |