JavaScript Operators: The Basics You Need to Know

When writing JavaScript programs, we often need to perform operations on values. For example, we might need to add numbers, compare two values, or check multiple conditions before making a decision.
To perform these tasks, JavaScript provides operators. An operator is a symbol that performs an operation on one or more values (called operands).
For example:
let sum = 5 + 3;
Here:
+ → operator
5,3 → operands
The operator performs an action on the values.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations. These are the most basic operators you will encounter in programming.
Example:
let a = 10;
let b = 5;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
Output:
15
5
50
2
0
The modulus operator % returns the remainder after division.
Example:
console.log(7 % 3);
Output:
1
Comparison Operators
Comparison operators are used to compare two values. They usually return a boolean value (true or false).
Difference between == and ===
This is an important concept in JavaScript.
== compares values only.
console.log(10 == "10");
Output:
true
Because JavaScript converts "10" to a number before comparison.
But === compares both value and type.
console.log(10 === "10");
Output:
false
Here:
10 → number
"10" → string
Since the types are different, the result is false.
For this reason, most developers usually prefer === over ==.
Logical Operators
Logical operators are used to combine multiple conditions.
Example:
let age = 20;
let hasID = true;
console.log(age > 18 && hasID);
Output:
true
Here both conditions must be true.
Example of OR operator:
console.log(age > 18 || age > 60);
Output:
true
The OR operator returns true if at least one condition is true.
Example of NOT operator:
console.log(!true);
Output:
false
The NOT operator simply reverses the boolean value.
Assignment Operators
Assignment operators are used to assign values to variables.
The most common assignment operator is:
=
Example:
let x = 10;
JavaScript also provides shorter assignment operators.
Example:
let x = 10;
x += 5;
console.log(x);
Output:
15
This is equivalent to writing:
x = x + 5;
Another example:
let y = 20;
y -= 5;
console.log(y);
Output:
15
Summary
Operators are an essential part of JavaScript programming. They allow us to perform mathematical calculations, compare values, combine conditions, and assign values to variables.
Understanding different types of operators such as arithmetic operators, comparison operators, logical operators, and assignment operators helps us write more powerful and expressive programs. As you continue learning JavaScript, you will use operators frequently in conditions, loops, and many other programming scenarios.




