Operators in Javascript
What is Operators
Operators are used to perform Operations. Now, we get a question, what is Operation. So, here are few example of Operations
- Add 2 Numbers
- Subtract 2 Numbers
- Compare 2 Numbers
- Assign a Value to a Variable
So, if you want to perform all these operation, you will need operators for that, and the Numbers or Variables, on which, you will perform, these operations, are called Operands. So, we have 3 terms with us.
- Operators
- Operands
- Operation
There are some predefined symbols for Operators that are being used, and we will discuss about those symbols in this session. Now, because Operotors use symbols, So If we want to define Oerators, then we can define in this way
Operators are Symbol, which are used to perform some operation on the operands.
Types of Operators in Javascript
We are going to discuss different types of operators in this session. We can categorize it in 2 Categories
- Type of Operators based on the Operations they perform
- Type of Operators based on the number of the operands they require
Based on the number of Operands required
- Binary Operators – Binary Operators require 2 Operands
- Unary Operators – Unary Operators require only 1 Operand
- Turnary Operators – Turnary Operators require 3 Operands
- Binary Operators can perform operation on variables as well as Constant.
- Unary Operators can perform operation only on variables.
Based on the Operations they perform
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operation on operands.
Let’s say we have 2 variables a and b, and we want to perform arithmetic operations on them.
Let’s say that the value of a and b is
let a = 10;
let b = 4;
Operators | Operation | Number of Operand | Syntax | Type |
+ | Addition (Add 2 Numbers) | 2 | a+b | Binary |
– | Subtraction (Subtract 2 Numbers) | 2 | a-b | Binary |
* | Multiplication (Multily 2 Numbers) | 2 | a*b | Binary |
/ | Division (Divide 2 Numbers) | 2 | a/b | Binary |
% | Modulus (Remainder) | 2 | a/b | Binary |
++ | Increment (Increase value by 1) | 1 | a++ | Unary |
— | Decrement (Decrease value by 1) | 1 | a++ | Unary |
Assignment Operators
Assignment Operators are used to perform assigment operations on the operands. We need atleast 1 Variable for peforming Assignment Operations. Let’s say we have 2 Variables a and b, and the values that they hold is.
let a = 10;
let b = 4;
Operators | Syntax | Operation | Number of Operand | Type |
= | a=b | Assigns value of left variable (b) to right variable(a) | 2 | Binary |
+= | a+=b | a=a+b (Add a and b and assign the result to a) | 2 | Binary |
-= | a-=b | a=a-b (Subtract b from a and assign the result to a) | 2 | Binary |
*= | a*=b | a=a*b (Multiply a and b and assign the result to a) | 2 | Binary |
/= | a/=b | a=a/b (Divide b from a and assign the result to a) | 2 | Binary |
%= | a%=b | a=a%b (Remainder of a/b is assigned to a) | 2 | Binary |
Comparison Operators
Comparison Operators compares 2 values and then returns either a true value or a false value. Comparison Operators return boolean values.
let’s say we have a variable a with a value of 7
let a = 7;
Operator | Syntax | Operation | Result | Type |
== | a==7 | Compares a with 7 for equal value | true | Binary |
a==”7″ | Compares a with “7” for equal value | true | Binary | |
a==8 | Compares a with 8 for equal value | false | Binary | |
=== | a===7 | Compares a with 7 for equal value and equal type | true | Binary |
a===”7″ | Compares a with “7” for equal value and equal type | false | Binary | |
a===8 | Compares a with 8 for equal value and equal type | false | Binary | |
!= | a!=7 | Compares a with 7 for not equal value | false | Binary |
a!=”7″ | Compares a with “7” for not equal value | false | Binary | |
a!=8 | Compares a with 8 for not equal value | true | Binary | |
!== | a!==7 | Compares a with 7 for not equal value and equal type | false | Binary |
a!==”7″ | Compares a with “7” for not equal value and equal type | true | Binary | |
a!==8 | Compares a with 8 for not equal value and equal type | true | Binary | |
> | a>7 | Compare if a is greater than 7 | false | Binary |
< | a<7 | Compare if a is less than 7 | false | Binary |
>= | a>= | Compare if a is greater than or equal to 7 | true | Binary |
<= | a<= | Compare if a is less than or equal to 7 | true | Binary |
Logical Operators
Logical Operators are the Operators that performs Logical Operation. Meaning of Logical Operation is Performing Operation on boolean values and returning a boolean value.
Operator | Description | Syntax | Result | Type |
&& | Logical AND | (true && true) | true | Binary |
(true && false) | false | Binary | ||
(false && false) | false | Binary | ||
|| | Logical OR | (true || true) | true | Binary |
(true || false) | true | Binary | ||
(false || false) | false | Binary | ||
! | Logical NOT | !true | false | Unary |
!false | true | Unary |
Summary
We have covered below topics in this session
- What is Operators
- Types of Operators
- Unary Operator
- Binary Operator
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator