JavaScript Operators: The Basics You Need to Know

What operators are ?
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. They are the building blocks of any logic you write
1. JavaScript Arithmetic Operators (+, -, *, /, %)
Arithmetic Operators perform mathematical calculations like addition, subtraction, multiplication, division, etc.
const sum = 5 + 3; // Addition
const diff = 6 - 2; // Subtraction
const p = 4 * 3; // Multipliaction
const q = 5 / 8; // Division
console.log(sum, diff, p, q);
adds two numbers.
subtracts the second number from the first.
multiplies two numbers. / divides the first number by the second.
2. JavaScript Comparison Operators
Comparison operators compare two values and return a boolean (true or false). They are useful for making decisions in conditional statements.
These operators are commonly used in conditions, such as inside if statements, to help a program make decisions.
console.log(10 > 5); // true
console.log(10 === "10"); // false
Explanation
>checks whether the value on the left is greater than the value on the right.- Example:
10 > 5returns true.
- Example:
===checks strict equality.It compares both the value and the data type.
Example:
10 === "10"returns false because10is a number and"10"is a string.
Other Comparison Operators
JavaScript also provides several other comparison operators:
| Operator | Meaning | Example |
|---|---|---|
< |
Less than | 5 < 10 |
<= |
Less than or equal to | 5 <= 5 |
>= |
Greater than or equal to | 10 >= 8 |
!== |
Not equal (strict) | 10 !== "10" |
In JavaScript, it's recommended to use === instead of == because === checks both the value and the data type, which prevents unexpected results.
Example:
console.log(10 == "10"); // true (type conversion happens)
console.log(10 === "10"); // false (types are different)
3. JavaScript Logical Operators
Logical operators are used to combine or modify Boolean values (true or false).
They are commonly used in conditional statements to check multiple conditions at the same time.
For example, you might want to check if two conditions are true, or if at least one condition is true.
Example
const a = true, b = false;
console.log(a && b); // Logical AND
console.log(a || b); // Logical OR
console.log(!a); // Logical NOT
&& (Logical AND)
This operator returns true only when both values are true.
true && true // true
true && false // false
Example:
console.log(true && true); // true
|| (Logical OR)
This operator returns true if at least one value is true.
true || false // true
false || false // false
Example:
console.log(true || false); // true
! (Logical NOT)
This operator changes the value to the opposite.
!true // false
!false // true
Example:
console.log(!true); // false
Small Example in Real Code
let age = 20;
let hasTicket = true;
if (age >= 18 && hasTicket) {
console.log("You can enter the event");
}
Here the message will print only if both conditions are true.
4. JavaScript Assignment Operators
Assignment Operators are used to assign values to variables. They can also perform operations like addition or multiplication while assigning the value.
The Assignment operators in JavaScript are:
Basic Assignment Operators (=)
Addition Assignment Operators (+=)
Subtraction Assignment Operators (-=)
- Basic Assignment Operators ( = ) :
The assignment ( = ) operators is used to assign a value to a variable or property.
Syntax: x = y
Code Example:
let num1 = 20;
let num2 = "Inder";
console.log(num1); //Output: 20
console.log(num2); //Output: "Inder"
- Addition Assignment Operators (+=):
The addition Assignment Operators perform addition (which is either numeric addition or string concatenation) on the two operands and assigns the result to the left operand.
Syntax : x += y
Code Example:
let num = 2; let pet = "hello";
console.log((num += 8)); // Addition (num = num + 8)
//Output: 10
console.log((pet += " My self dog"));
// Concatenation (pet = pet + "My self dog")
//Output: "hello My self dog"
- Subtraction Assignment Operator
(-=)
The subtraction assignment (-=) operator performs subtraction on the two operands and assigns the result to the left operand.
Syntax: x -= y
Code Example:
let num2 = 11;
console.log(a -= 1);
// Output: 10
console.log(a -= "kuku");
// you cannot mathematically subtract a string.
// Output: NaN
Assignment Idea
1 . Perform arithmetic operations on two numbers
Code Example:
let num1 = 20;
let num2 = 2;
let add = num1 + num2;
console.log(add); //Output: 22
let sub = num1 - num2;
console.log(sub); //Output:18
let multi= num1 * num2;
console.log(multi); //Output:40
let dev = num1 / num2;
console.log(dev); //Output:10
let mod = num1 % num2;
console.log(mod); //Output:0
let inc = num1++;
console.log(inc); //Output:21
let dec = num2--;
comsole.log(dec); //Output:1
2 . Compare two values using both == and ===
Code Example:
let num1 = 5;
let num2 = "5";
//Loose Equality Operator
let bothEqual = num1 == num2;
console.log(bothEqual); //Output: true
//Strict Equality Operator
let bothStriclyEqual = num1 === num2;
console.log(bothStriclyEqual); //Output: false
3 . Write a small condition using logical operator
Code Example:
let num1 = 10;
let num2 = 6;
//Logical AND (&&) Operator
if (num1 > 0 && num2 > 15) {
console.log("Both the conditions are false!"); //Output: false
}
//Logical OR (||) Operator
if(num1 > 2 || num2 < 90) {
console.log("At least one condition is true"); //Output: true
}
//Logical NOT (!) Operator
if(typeof num1 !== "number") {
console.log("x is not a number"); //Output: false
}
Conclusion
JavaScript operators are a basic but very important part of coding.
They help you do calculations, compare values, and control how your program works.
At first, they may look simple, but they are used in almost every line of code you write.