What are Operators ?Operators are some mathematical symbol that can be used to perform an operation between variables.
Types of Operators ?
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator
- Ternary Operator
- Bitwise Operator
Arithmetic Operators
Arithmetic operations refer to basic mathematical procedures used to manipulate numbers. These operations are foundational in mathematics and are commonly used for calculations in everyday life, science, engineering, and various fields.
let a = 10
let b = 2
console.log(a + b)
console.log(a – b)
console.log(a * b)
console.log(a / b)
console.log(a % b)
console.log(a ** b)
console.log(a++)
console.log(a–)
console.log(++a)
console.log(–a)
Assignment Operator
It is used to assign the value to a variable. The = we use to initialize a variable’s value is called assignment operator.
let d = 20
console.log(d += 5)
console.log(d -= 2)
console.log(d *= 2)
console.log(d /= 2)
console.log(d %= 2)
console.log(d ** 2)
Can you guess the value of d ??????????
Comparison Operator
It is used to compare the values and returns a boolean expression i.e. either true or false
let x = 20
let y = 10
console.log(x == y)
console.log(x != y)
console.log(x === y)
console.log(x !== y)
console.log(x > y)
console.log(x < y)
console.log(x >= y)
console.log(x <= y)
Logical Operator
It is used to perform logical operations, they are often use in control flow statements like if and else and conditions to determine how code behaves based on boolean values.
It includes AND OR and NOT
Logical AND&& represents AND operator, meaning every condition must be satisfied to perform a statement.Logical OR|| represents OR operator, meaning any of the condition should satisfy the criteria.Logical NOT! represents the NOT operator, meaning reverse of the boolean expression. If it is true, do false and if false, do true.
let j = 20
let g = 10
console.log(j > 100 && g < 100)
console.log(j > 100 || g < 100)
console.log(!true)
Ternary Operator
They are the substitute for if else conditions. we use ? to denote the if and : to denote the else
let a = 20
if (a < 100){
console.log(“higher”)
}
else{
console.log(“lower”)
}
let a = 20
(a < 100) ? console.log(“higher”) : console.log(“lower”)
Bitwise Operator
- They operate at binary level.
- use for bit manipulation
- These are useful for tasks when working with low level data structures
- Manipulating individual bits within an integer.
Bitwise AND& represent the bitwise AND operator. IF a bit is set to 1 as both operands then the result will be 1. IF any of the corresponding bit is 0 then it will set as 0BItwise OR| represents the bitwise OR operator. If any of the bit are set to 1 then the result will be 1 and if both bits are set to 0 then it returns 0 as wellBitwise NOT~ represents the bitwise NOT operator. It inverts the bits. 1 becomes 0 and 0 becomes 1Bitwise XOR^ represents bitwise XOR. This operator returns a number where each bit is set to 1 if only one of the corresponding bits in the operands is 1. If both bits are the same, it’s set to 0.Bitwise left shift<< represents bitwise left shift. This operator shifts the bits to the left by a specified number of places, filling in the new bits on the right with 0s.Bitwise right shift>> represents bitwise right shift. This operator shifts the bits to the right by a specified number of places, retaining the sign bit for signed integers.Bitwise Zero-fill right shift>> represents bitwise zero fill right shift. This operator shifts the bits to the right by a specified number of places, filling in the new bits on the left with 0s, regardless of the sign bit.
const a = 5;
const b = 3;
const result = a & b;
const result = a | b;
const result = a ^ b;
const result = ~a;
const result = a << 1;
const x = 10;
const result = x >> 1;
const p = -10;
const result = p >>> 1;
You have Learned JavaScript operators……………..
If you want more such content then make sure to following me on all available platform
Instagram
LinkedIn
Hashnode
Thank you for reading 🤍
Written By Mohit Soni
Source: hashnode.com