JavaScript Operators: The Basics You Need to Know

Introduction to Operators :
When writing code in JavaScript, you’ll often need to perform operations whether it’s adding numbers, comparing values, or combining logic. That’s where operators come in.
An operator is simply a symbol that tells JavaScript to perform a specific action on one or more operands (which can be values, variables, or expressions).
let sum = 10 + 5; // '+' is the addition operator
Defination : In JavaScript, operators are special symbols or keywords that perform operations on values or variables (operands). They are essential for performing mathematical calculations, comparing data, making logical decisions, and assigning values.
Types of Operators in JavaScript :
JavaScript provides many types of operators to perform different kinds of tasks. Let’s go through the main ones:
1. Arithmetic Operators :
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 2 |
7 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 5 * 2 |
10 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Here are the some example of the "Arithmetic Operator":
- Addition: It adds two numbers.
let a = 23;
let b = 54;
let c = a + b;
console.log(`Sum of number is ${c}`);
Output:
Sum of number is 77
=== Code Execution Successful ===
- Subtraction: It subtracts one number from another
let a = 23;
let b = 54;
let c = a - b;
console.log(`Sum of number is ${c}`);
Output:
sum of number is -31
=== Code Execution Successful ===
- Multiplication : Multiplies two numbers.
let a = 23;
let b = 54;
let c = a * b;
console.log(`Sum of number is ${c}`);
Output:
sum of number is 1242
=== Code Execution Successful ===
- Division : Divides one number by another.
let a = 10;
let b = 5;
let result = a / b;
console.log(`Value is : ${result}`);
Output:
Value is : 2
=== Code Execution Successful ===
- Modulus (Remainder): Returns the remainder after division.
let a = 10;
let b = 3;
let result = a % b;
console.log(`Value is : ${result}`);
Output:
Value is : 1
=== Code Execution Successful ===
- ExponentiationExponentiation : It is power operator.
let result = 2 ** 3;
console.log(result);
Output:
8
2. Comparison Operators
Comparison operators compare two values and return a boolean (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to (checks value only) | 5 == "5" |
true |
=== |
Strict equal (checks value & type) | 5 === "5" |
false |
!= |
Not equal to (checks value only) | 5 != "6" |
true |
!== |
Strict not equal | 5 !== "5" |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 2 < 5 |
true |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 4 <= 3 |
false |
Here are the some example of the "Comparison Operator" :
let x = 10;
let y = 5;
let z = "10";
console.log(x == z); // true Equal to (checks value only)
console.log(x === z); // false Strict equal (checks value & type)
console.log(x != y); // true Not equal to (checks value only)
console.log(x !== z); // true Strict not equal
console.log(x > y); // true Greater than
console.log(x < y); // false Less then
console.log(x >= 10); // true Greater than or equal to
console.log(x <= 5); // false Less than or equal to
Here's an real life example of Comparison Operator :
let userAge = 28;
if (userAge >= 18) {
console.log("You can vote");
} else {
console.log("You cannot vote");
}
Output:
You can vote
3. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
&& |
Logical AND | Returns true if both conditions are true |
true && false |
false |
| ` | ` | Logical OR | Returns true if at least one condition is true |
|
! |
Logical NOT | Reverses the boolean value | !true |
false |
Here are the some example of the "Logical Operator" :
- Logical AND : Returns
trueonly if both conditions are true
let age = 22;
let hasLicense = false;
if (age >= 18 && hasLicense) {
console.log("You can drive");
} else {
console.log("You cannot drive");
}
Output:
You cannot drive // In this code both input have to be true, than this code work .
- Logical OR : Returns
trueif at least one condition is true
let age = 22;
let hasLicense = false;
if (age >= 18 || hasLicense) {
console.log("You can drive");
} else {
console.log("You cannot drive");
}
Output :
You can drive
=== Code Execution Successful === // In this code only one condition is true but it still run
- Logical NOT : Reverses the boolean value.
let isLoggedIn = true;
console.log(!isLoggedIn); // It convert boolean value from true to false.
Output :
false
=== Code Execution Successful ===
4. Assignment Operators
Assignment operators are used to assign values to variables. The most common one is =, but there are shorthand forms for performing operations and assigning at the same time.
| Operator | Description | Example | Equivalent To |
|---|---|---|---|
= |
Assigns a value | x = 5 |
— |
+= |
Adds and assigns | x += 5 |
x = x + 5 |
-= |
Subtracts and assigns | x -= 5 |
x = x - 5 |
*= |
Multiplies and assigns | x *= 5 |
x = x * 5 |
/= |
Divides and assigns | x /= 5 |
x = x / 5 |
%= |
Modulus and assigns | x %= 2 |
x = x % 2 |
Here is an example of the "Assignment Operator"
let salary = 10000; // IN this first you created a variable named salary and stored 10000 in it.
console.log("Initial salary:", salary);
salary += 2000; // In this you add and assign 2000 in salary.
console.log("After += :", salary);
salary -= 1500; // In this you subtract and assign 1500 in salary.
console.log("After -= :", salary);
salary *= 2; // In this you multiply and assign the salary by 2
console.log("After *= :", salary);
salary /= 3; // In this you divide and assign the salary by 3.
console.log("After /= :", salary);
salary %= 4000; // In this you do modulus and assign of salary by 4000.
console.log("After %= :", salary);
salary **= 2; // In this you did the exponent and assign the salary by 2
console.log("After **= :", salary);
Output:
Initial salary: 10000
After += : 12000
After -= : 10500
After *= : 21000
After /= : 7000
After %= : 3000
After **= : 9000000
=== Code Execution Successful ===
Difference between == and === :
Number vs String:
let a = 10;
let b = "10";
console.log(a == b); // true
console.log(a === b); // false
Why?
== converts "10" --> 10
So comparison becomes:
10 == 10 // true
But === checks datatype too:
number === string // false
Here is an theoretical Difference between == and === in JavaScript
| Aspect | == (Loose Equality) |
=== (Strict Equality) |
|---|---|---|
| Type Coercion | Yes, performs type coercion | No, does not perform type coercion |
| Type Checking | Compares value after conversion | Compares both value and datatype |
| Predictability | Can give unexpected results sometimes | More predictable and reliable |
| Recommendation | Use only when type conversion is needed intentionally | Recommended for most cases (best practice) |
Here is an example on JavaScript all Assignment Operators :
let num1 = 10;
let num2 = 5;
console.log("Addition:", num1 + num2);
console.log("Subtraction:", num1 - num2);
console.log("Multiplication:", num1 * num2);
console.log("Division:", num1 / num2);
console.log("Modulus:", num1 % num2);
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
=== Code Execution Successful ===
Here is an comparison of values using == and === :
let a = 10;
let b = "10";
console.log("Using == :", a == b);
console.log("Using === :", a === b);
Output:
Using == : true
Using === : false
=== Code Execution Successful ===
Here is an simple code using all operator :
let x = 15;
let y = "15";
// here's an arithmetic
console.log("Sum:", x + Number(y));
// Comparison
console.log("Loose equality:", x == y);
console.log("Strict equality:", x === y);
// Logical condition
let isAdult = x >= 18;
let hasID = true;
console.log("Access allowed:", isAdult || hasID);
Output:
Sum: 30
Loose equality: true
Strict equality: false
Access allowed: true
=== Code Execution Successful ===
Conclusion on Operators in JavaScript
Operators are the foundation of logic and calculations in JavaScript. They allow us to perform mathematical operations, compare values, assign data, and control program flow using conditions.
Arithmetic operators help perform calculations like addition, subtraction, multiplication, and division.
Assignment operators help store and update values in variables efficiently.
Comparison operators help compare values and return
trueorfalse.Logical operators help combine multiple conditions and make decisions.
Together, these operators make JavaScript powerful and enable developers to build real-world features like login systems, calculators, validations, and dynamic applications.




