JavaScript Operators: The Basics You Need to Know
When writing programs, we constantly need to perform operations such as adding numbers, comparing values, or making decisions based on conditions. In JavaScript, operators allow us to perform these tasks easily.
In this article, we'll understand what operators are and explore some of the most commonly used types in JavaScript.
What Are Operators?
Operators are special symbols used to perform operations on values (called operands).
For example:
let result = 5 + 3;
Here:
+is the operator5and3are operandsThe result becomes
8
Operators help us perform calculations, comparisons, and logical decisions in our programs.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Common Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 2 |
| - | Subtraction | 5 - 2 |
| * | Multiplication | 5 * 2 |
| / | Division | 10 / 2 |
| % | Modulus (remainder) | 10 % 3 |
Example
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1
The modulus (%) operator returns the remainder after division.
Comparison Operators
Comparison operators are used to compare two values.
They return either true or false.
Common Comparison Operators
| Operator | Meaning |
|---|---|
| == | Equal to |
| === | Strict equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
Example
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Difference Between == and ===
This is one of the most important concepts in JavaScript.
== compares values only.
console.log(5 == "5"); // true
JavaScript converts the string "5" to the number 5, so the result becomes true.
=== compares both value and type.
console.log(5 === "5"); // false
Here the values look the same, but the types are different:
5→ number"5"→ string
So the result is false.
In modern JavaScript, developers usually prefer === because it avoids unexpected type conversions.
Logical Operators
Logical operators are used to combine multiple conditions.
Common Logical Operators
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
Examples
let age = 20;
let hasID = true;
console.log(age > 18 && hasID);
// true (both conditions are true)
console.log(age < 18 || hasID);
// true (one condition is true)
console.log(!hasID);
// false (NOT reverses the value)
Logical operators are very useful in decision-making conditions.
Assignment Operators
Assignment operators are used to assign values to variables.
Common Assignment Operators
| Operator | Meaning |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
Examples
let score = 10;
score += 5;
console.log(score); // 15
score -= 3;
console.log(score); // 12
+= and -= are shortcuts that make code shorter and cleaner.
Small Practice Example
Let’s combine everything we learned.
let num1 = 8;
let num2 = 4;
// Arithmetic
console.log(num1 + num2); // 12
console.log(num1 * num2); // 32
// Comparison
console.log(num1 == "8"); // true
console.log(num1 === "8"); // false
// Logical condition
let result = num1 > num2 && num2 > 2;
console.log(result); // true
This small example shows how arithmetic, comparison, and logical operators work together.
Conclusion
Operators are one of the fundamental building blocks of JavaScript. They allow us to perform calculations, compare values, and make decisions in our code.
In this article we covered:
What operators are
Arithmetic operators (
+,-,*,/,%)Comparison operators (
==,===,!=,>,<)Logical operators (
&&,||,!)Assignment operators (
=,+=,-=)
Mastering these operators will make it much easier to write effective JavaScript programs.
As you continue learning, you’ll use these operators in almost every piece of code you write.
Happy coding!