Operators in JS:
- In JavaScript, operators are symbols or keywords used to perform operations on values (operands).
- They are the building blocks of expressions and allow you to manipulate data, perform calculations, compare values, and control logic.
- Operators fall into three arities based on how many operands they take:
- Binary takes two operands, one on each side: a + b, x === y
- Unary takes a single operand, before or after: typeof x, x++
- Ternary takes three operands: condition ? a : b
Types of Operators in JS:
JavaScript provides a variety of operators to perform actions on values and variables, and data types to represent different kinds of values.
1. Arithmetic Operators:
- Arithmetic Operators perform mathematical calculations like addition, subtraction, multiplication, division, remainder, increment, decrement,exponentiation etc.
example:
let x = 5;
console.log("Addition: x + 3 = ", x + 3);
console.log("Subtraction: x - 3 =", x - 3);
console.log("Multiplication: x * 3 =", x * 3);
console.log("Division: x / 3 =", x / 3);
console.log("Remainder: x % 3 =", x % 3);
console.log("Increment: ++x =", ++x);
console.log("Decrement: --x =", --x);
console.log("Exponentiation: x ** 3 =", x ** 3);
output:
Addition: x + 3 = 8
Subtraction: x - 3 = 2
Multiplication: x * 3 = 15
Division: x / 3 = 1.6666666666666667
Remainder: x % 3 = 2
Increment: ++x = 6
Decrement: --x = 5
Exponentiation: x ** 3 = 125
2. Assignment Operators:
- Assignment operators are used to assign values to variables.
- They can also perform operations like addition or multiplication while assigning the value.
- For example: a += 5 -> a = a + 5.
example:
let a = 7;
console.log(a);
a += 5;
console.log(a);
a -= 5;
console.log(a);
a *= 2;
console.log(a);
a /= 2;
console.log(a);
a %= 2;
console.log(a);
a **= 2;
console.log(a);
output:
7
12
7
14
7
1
1
3. Comparison Operators:
- Comparison operators compare two values and return a boolean (true or false).
- They are useful for making decisions in conditional statements.
- Loose equality (==)
- Checks whether two values are equal after type coercion. JavaScript converts operand types before comparing.
- Strict equality (===)
- Checks value and type. No coercion happens. Prefer === in almost every situation because loose equality produces counterintuitive results.
- Loose inequality (!=)
- Returns true if the values are not equal after coercion.
- Greater than (>) and greater than or equal (>=)
- Less than (<) and less than or equal (<=)
example:
console.log(2 == 2);
console.log(3 != 3);
console.log(2 === '2');
console.log(2 !== '2');
console.log(3 > 3);
console.log(2 > 2);
console.log(3 >= 3);
console.log(2 <= 2);
output:
true
false
false
true
false
false
true
true
4. Logical Operators:
- Logical operators are mainly used to perform the logical operations that determine the equality or difference between the values.
- && returns true if both operands are true.
- || returns true if at least one operand is true.
- ! negates the boolean value.
example:
let age = 20;
console.log(age > 18 && age < 30);
console.log(age > 18 || age > 30);
console.log(!(age > 18));
output:
true
true
false
5. Bitwise Operators:(TBD)
- Bitwise operators perform operations on binary representations of numbers.
- & performs AND operation on each bit.
- | performs OR operation on each bit.
- ^ performs XOR (exclusive OR) on each bit.
- ~ inverts all bits (NOT operator).
- << shifts bits to the left.
- >> shifts bits to the right (with sign).
- >>> shifts bits to the right (without sign).
6. Ternary Operator:
- A ternary operator evaluates a condition and executes a block of code based on the condition.
- The ternary operator evaluates the test condition.
- If the condition is true, expression1 is executed.
- If the condition is false, expression2 is executed.
- The ternary operator takes three operands, hence, the name ternary operator.
- It is also known as a conditional operator.
syntax:
condition ? expression1 : expression2
example:
let marks = 45;
let result = marks>35 ? "pass" : "fail";
console.log(result)
output:
pass
7. Type Operators:
- typeof → Returns the type of a variable.
- instanceof → Checks if an object is an instance of a class.(TBD)
example:
console.log(typeof "Hello");
console.log([1,2,3] instanceof Array);
output:
string
true
References:
Top comments (0)