Operators In JavaScript
JavaScript operators are symbols that are used to perform operations on operands.
They are things like addition +, multiplication *, subtraction -, and so on.
In JavaScript we have different types of operators such as
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Special Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables and/or values.
let y = 5; let x = y + 2; console.log(x); // it wil return 7
Similarly we can calculate using different operators such as multiplication, Subtraction and division. There are also some special operators such as :
Exponential Operators
It is an updated operator from (ES6)
let y = 2; let x = y ** 2; console.log(x); // it will return 4;
Increment
The increment symbol is displayed as ++
let y = 5; let x = ++y; console.log(x); // it will return 6 console.log(y); it will return 6
Note that both the variables will be affected here. The Decrement operator works as same.
Assignment Operators
Some of the assignment operators are like :
● =
● +=
● -=
● *=
● %=
The examples of assignment operators are mentioned below :
● let x = 10; let y = 5; x = y; console.log(x) // it will return 10 ● let x = 10; let y = 5; x += y; console.log(x); // it will return 15 ● let x = 10; let y = 5; x *= y; console.log(x); // it will return 15 ●let x = 10; let y = 5; x %= y; console.log(x); // it will return the 15
Similarly we can use the above assignment operators
Comparison (Relational )Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
X = 5; console.log(x == 8); // returns false X = 5 console.log(x == 5) // returns true X = 5 console.log(x === “5”) ; // returns false as three equal to check for the data type as well. If x is integer 5 or string 5 , So it returns false. X = 5 console.log(x != 8) // returns true as this stands for not equal to ,So it returns true. X = 5 console.log(x ! ===”8”)// returns true. Here it stands for not equal to and not equal data type. X = 5 console.log(x > 8) ; // returns false ,it stands for greater than. X = 5 console.log(x >= 8); // it returns false as it checks is variable is greater than ir equal to.
Logical Operators
Logical operators are used to determine the logic between variables or values. Logical operators include
● && and
● || or
● ! not
Below are the mentioned examples
let x = 6; let y = 3; console.log(x < 10 && y > 1); // return true as variable x is less than 10 and variable y is greater than 1. let x = 6; let y = 3; console.log(x == 5 || y == 5); // returns false as variable x is not equal to 5 and variable y is not equal to 5. Here we are using || or condition to check the values of both the variables at once. let x = 6; let y = 3; console.log(!x === y) // return true
The NOT operator (!) returns true for false statements and false for true statements.
Special Operators
Below are some of the examples of special operators in js
Ternary operator
This operator can be used as a shortcut for if statement. It Returns the value of expr1 if condition is true, or the value of expr2 if condition is false
Condition ? exp1 : exp2
Comma operator (,)
exp1,exp2
This operator evaluates both the expressions and returns value of exp2. This operator can be used to put multiple expressions in one statement.
Nullish Operator (??)
The ?? operator returns the first argument if it is null or undefined. Otherwise it returns the second.
let a = 3 let b = null console.log(a ?? b) // it returns 3 as it replaces the b null value with a value 3 . let a = undefined let b = 3 console.log(a ?? b ) // it returns 3 as it replaces the value of undefined with 3 .