Javascript Operators The Basic You need to Know :

Understanding Operators in JavaScript
While writing programs, we frequently perform operations on different values.
For example:
Adding two numbers
Comparing values
Updating a variable’s value
Combining multiple conditions
In JavaScript, the symbols used to perform these operations are called operators.
Operators are extremely important because they appear in almost every program we write.
1. What Are Operators?
An operator is a symbol that performs an operation on values.
The values on which the operator works are called operands.
Example:
let total = 7 + 3
Here:
7and3→ operands+→ operator
The + symbol tells JavaScript to add the two numbers together.
Operators allow programs to perform calculations, compare values, and control program logic.
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
These operations are similar to the basic math we use daily.
| Operator | Purpose |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
Addition (+)
The addition operator adds two numbers.
Example:
let x = 12
let y = 8
console.log(x + y)
Output:
20
The + operator is widely used in programming.
Subtraction (-)
The subtraction operator finds the difference between two numbers.
Example:
let x = 15
let y = 6
console.log(x - y)
Output:
9
Multiplication (*)
Multiplication is used to multiply numbers.
Example:
let x = 5
let y = 7
console.log(x * y)
Output:
35
Division (/)
Division splits one number by another.
Example:
let x = 30
let y = 6
console.log(x / y)
Output:
5
Modulus (%)
The modulus operator gives the remainder after division.
Example:
let x = 11
let y = 4
console.log(x % y)
Output:
3
Explanation:
11 ÷ 4 = 2 remainder 3
Practical Example
This operator is often used to check whether a number is even or odd.
let value = 12
console.log(value % 2)
Output:
0
If the remainder is 0, the number is even.
3. Comparison Operators
Comparison operators are used to compare two values.
They return a boolean value, which can be:
truefalse
| Operator | Meaning |
|---|---|
| == | Equal (loose comparison) |
| === | Strict equality |
| != | Not equal |
| > | Greater than |
| < | Less than |
Equal To (==)
The == operator checks whether two values are equal after converting their types if necessary.
Example:
console.log(8 == "8")
Output:
true
Why?
JavaScript converts the string "8" into the number 8 before comparing.
8 == "8"
8 == 8
true
Because of this automatic conversion, == can sometimes produce unexpected results.
Strict Equal (===)
The === operator checks both the value and the data type.
Example:
console.log(8 === "8")
Output:
false
Explanation:
8→ number"8"→ string
Since the data types are different, the result becomes false.
Difference Between and =
Example:
console.log(20 == "20")
console.log(20 === "20")
Output:
true
false
| Operator | What it checks |
|---|---|
| == | Value only |
| === | Value and type |
Best Practice
In modern JavaScript, developers usually prefer using === because it avoids unexpected type conversions.
Not Equal (!=)
This operator checks if two values are different.
Example:
console.log(7 != 4)
Output:
true
Because 7 and 4 are not equal.
Greater Than (>) and Less Than (<)
These operators compare numbers.
Example:
console.log(18 > 10)
Output:
true
Another example:
console.log(3 < 1)
Output:
false
4. Logical Operators
Logical operators are used when working with multiple conditions together.
They are commonly used in decision-making statements like if conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| ! | Logical NOT |
Logical AND (&&)
The AND operator returns true only if both conditions are true.
Example:
let temperature = 25
let isSunny = true
console.log(temperature > 20 && isSunny)
Output:
true
Explanation:
temperature > 20→ trueisSunny→ true
Both conditions are true, so the final result is true.
Logical OR (||)
The OR operator returns true if at least one condition is true.
Example:
let hasCoupon = false
let isMember = true
console.log(hasCoupon || isMember)
Output:
true
Since one condition is true, the result becomes true.
Logical NOT (!)
The NOT operator simply reverses a boolean value.
Example:
let isOnline = true
console.log(!isOnline)
Output:
false
5. Assignment Operators
Assignment operators are used to assign values to variables.
The most basic assignment operator is:
=
Basic Assignment (=)
Example:
let points = 50
Here the value 50 is assigned to the variable points.
Addition Assignment (+=)
This operator adds a value to the variable and updates it.
Example:
let points = 10
points += 3
This is the same as writing:
points = points + 3
Final value:
13
Subtraction Assignment (-=)
This operator subtracts a value from a variable and updates it.
Example:
let points = 25
points -= 5
Equivalent to:
points = points - 5
Final value:
20
These shorthand operators make the code shorter and easier to read.



