learn2kode.in

JavaScript Operators

JavaScript operators are symbols that allow you to perform actions on values and variables.
You use operators to calculate numbers, compare values, assign data, and more.

Example

Here + is an operator.

Types of JavaScript Operators

JavaScript has several types of operators:

Let’s learn each one with examples.

1. Arithmetic Operators

Used to perform mathematical calculations.

Operator Description Example (x = 10, y = 5) Result
+ Addition x + y 15
- Subtraction x - y 5
* Multiplication x * y 50
/ Division x / y 2
% Modulus (remainder) x % y 0
** Exponentiation x ** 2 100
++ Increment x++ 10 → 11
-- Decrement x-- 10 → 9
Example
let a = 7;
let b = 3;
console.log(a % b); // 1
2. Assignment Operators

Used to assign values to variables.

Operator Meaning Example Equivalent to
= Assign x = 5
+= Add & assign x += 3 x = x + 3
-= Subtract & assign x -= 2 x = x - 2
*= Multiply & assign x *= 4 x = x * 4
/= Divide & assign x /= 5 x = x / 5
%= Modulus & assign x %= 2 x = x % 2
Example
let x = 10;
x += 5;
console.log(x); // 15
3. Comparison Operators

Used to compare values. They return true or false.

Operator Description Example Result
== Equal (value only) 5 == "5" true
=== Strict equal (value + type) 5 === "5" false
!= Not equal 5 != 3 true
!== Strict not equal 5 !== "5" true
> Greater than 10 > 5 true
< Less than 3 < 1 false
>= Greater or equal 10 >= 10 true
<= Less or equal 5 <= 4 false
Important:

👉 Always prefer === instead of == to avoid unexpected type conversion.

4. Logical Operators

Used to combine multiple conditions.

Operator Name Example Result
&& AND age >= 18 && hasID true (both conditions are true)
|| OR age >= 18 || hasID true (at least one condition is true)
! NOT !hasID false (opposite of true)
Example
let age = 20;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("Access granted");
}
5. String Operators

Only one main operator: + (plus) for string concatenation.

Example
let first = "Learn2";
let second = "Kode";
console.log(first + second); // Learn2Kode
+= also works:
let text = "Hello";
text += " World";
console.log(text); // Hello World
6. Unary Operators

Operators that work with one operand.

Operator Use Example
typeof Check data type typeof 10"number"
! NOT !true → false
+ Convert to number +"10" → 10
- Negative -5 → -5
7. Ternary Operator (?:)

Shortcut for if...else.

Syntax
condition ? valueIfTrue : valueIfFalse
Example
let age = 18;
let message = (age >= 18) ? "Adult" : "Minor";
console.log(message);

Final Summary

Type Purpose
Arithmetic Math calculations
Assignment Update/assign values
Comparison Compare values
Logical Combine conditions
String Combine text
Unary Work with one value
Ternary Short if-else