JavaScript Operators
In this article we learn about the different types of operators in js, how to use them with different types of operands and how the operators behave differently with different data types.
What Are Operators?
JavaScript operators are symbols or keywords used to perform operations on values and variables. They are the building blocks of JavaScript expressions and can manipulate data in various ways.
Arithmetic Operators
Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, etc.
const sum = 5 + 3; // Addition
const diff = 10 - 2; // Subtraction
const p = 4 * 2; // Multiplication
const q = 8 / 2; // Division
console.log(sum, diff, p, q);
Assignment Operators
Assignment Operators are used to assign values to variables. They can also perform operations like addition or multiplication while assigning the value.
let n = 10;
n += 5;
n *= 2;
console.log(n);
+= is the short hand operator which adds and assigns the result to the variable.
-= is the short hand operator which subtracts and assigns the result to the variable.
and same for the rest like (*= , /+ etc.)
Comparison Operators
Comparison operators compare two values and return a boolean (true or false). They are useful for making decisions in conditional statements.
console.log(10 > 5);
console.log(10 === "10");
-- > checks if the left value is greater than the right.
-- === checks for strict equality (both type and value).
-- Other operators include <, <=, >=, and !==.
Logical Operators
Logical operators are mainly used to perform the logical operations that determine the equality or difference between the values.
const a = true, b = false;
console.log(a && b); // Logical AND
console.log(a || b); // Logical OR
-- && returns true if both operands are true.
-- || returns true if at least one operand is true.
-- ! negates the boolean value.
Bitwise Operators
Bitwise Operators perform operations on binary representations of numbers.
const res = 5 & 1; // Bitwise AND
console.log(res);
-- & performs a bitwise AND.
-- | performs a bitwise OR.
-- ^ performs a bitwise XOR.
-- ~ performs a bitwise NOT.
Difference between == and ===
== compares values after converting their types if needed.
=== compares both value and type.
console.log(5 == "5"); // true
// == does not check the types of the both operands, olny check the value
console.log(5 === "5"); // false
// === does check both the types and value of the operands
Conclusion
Operators are fundamental building blocks in JavaScript that allow us to perform calculations, compare values, and control program logic.
In this article, we explored several types of operators, including:
Arithmetic operators for performing calculations
Comparison operators for evaluating conditions
Logical operators for combining conditions
Assignment operators for storing and updating values
BItwise operators for the binary-level operations



