DEV Community

Cover image for Operators in JavaScript
Kesavarthini
Kesavarthini

Posted on

Operators in JavaScript

📌 What are Operators?

Operators are symbols used to perform operations on variables and values.

👉 Example:

let result = 10 + 5;
Enter fullscreen mode Exit fullscreen mode

Here, + is an operator.

🧠 Types of Operators in JavaScript

JavaScript provides different types of operators:

🔹 1. Arithmetic Operators

Used for mathematical calculations.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
++ Increment
-- Decrement

Example:

let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a % b); // 1

Enter fullscreen mode Exit fullscreen mode

🔹 2. Assignment Operators

Used to assign values to variables.

Operator Example
= x = 10
+= x += 5
-= x -= 2
*= x *= 3
/= x /= 2

Example:

let x = 10;
x += 5; // x = 15
Enter fullscreen mode Exit fullscreen mode

🔹 3. Comparison Operators

Used to compare values.

Operator Description
== Equal (value only)
=== Strict equal (value + type)
!= Not equal
> Greater than
< Less than

Example:

console.log(5 == "5");   // true
console.log(5 === "5");  // false
Enter fullscreen mode Exit fullscreen mode

🔹 4. Logical Operators

Used to combine conditions.

1. AND (&&)

👉 Returns true only if both conditions are true

✅ Example:

let a = 10;
let b = 5;

console.log(a > 5 && b > 2);  // true
console.log(a > 5 && b > 10); // false
Enter fullscreen mode Exit fullscreen mode

2. OR (||)

👉 Returns true if at least one condition is true

✅ Example:

let a = 10;
let b = 5;

console.log(a > 5 || b > 10); // true
console.log(a < 5 || b > 10); // false
Enter fullscreen mode Exit fullscreen mode

3. NOT (!)

👉 Reverses the result

✅ Example:

let isStudent = true;

console.log(!isStudent); // false
Enter fullscreen mode Exit fullscreen mode

🔹 5. Unary Operators

Works with a single value.

let x = 5;

console.log(++x); // 6
console.log(--x); // 5
Enter fullscreen mode Exit fullscreen mode

🔹 6. Ternary Operator

Shortcut for if-else condition.

let age = 18;

let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);
Enter fullscreen mode Exit fullscreen mode

📌 What is a Type Operator?

A type operator is used to find the data type of a variable.

🔹 typeof Operator

typeof is used to check the type of a value.

✅ Examples:

console.log(typeof 10);        // number
console.log(typeof "Hello");   // string
console.log(typeof true);      // boolean
console.log(typeof undefined); // undefined
console.log(typeof null);      // object (special case)
Enter fullscreen mode Exit fullscreen mode

📌 What is Type Coercion?

Type coercion means:

👉 Automatic conversion of one data type to another by JavaScript

🔄 Types of Coercion
🔹 1. Implicit Coercion (Automatic)

JavaScript converts types automatically.

Example:

console.log("5" + 2);  // "52"
console.log("5" - 2);  // 3
console.log("5" * 2);  // 10
Enter fullscreen mode Exit fullscreen mode

👉 + converts to string
👉 -, * convert to number

🔹 2. Explicit Coercion (Manual)

We manually convert types.

Example:

Number("10");   // 10
String(100);    // "100"
Boolean(1);     // true
Enter fullscreen mode Exit fullscreen mode

🔥 Common Examples

console.log(true + 1);   // 2
console.log(false + 1);  // 1
console.log(null + 1);   // 1
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Rules
"5" + 2 → String
"5" - 2 → Number
true → 1
false → 0

Top comments (0)