JavaScript Operators

1. What are Operators?

Operators are special symbols used to perform operations on operands (values and variables). For example, in 10 + 5, the + is the operator and 10 and 5 are operands.

2. Arithmetic Operators

Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, etc.

Example

let a = 10;
let b = 3;

console.log("Addition: " + (a + b));       // 13
console.log("Subtraction: " + (a - b));    // 7
console.log("Multiplication: " + (a * b)); // 30
console.log("Division: " + (a / b));       // 3.333...
console.log("Modulus: " + (a % b));        // 1
console.log("Exponentiation: " + (a ** b));// 1000
Output will appear here...

Increment & Decrement

Prefix (++x): Increments the value, then returns it.
Postfix (x++): Returns the value, then increments it.

let x = 5;
console.log(x++); // 5 (Returns original value, then increments)
console.log(x);   // 6

let y = 5;
console.log(++y); // 6 (Increments, then returns new value)
        
Output will appear here...

3. Assignment Operators

Assignment operators assign values to JavaScript variables. The most common one is =.

let x = 10; // Assignment

x += 5;  // Same as x = x + 5
console.log("x += 5 ->", x); // 15

x -= 2;  // Same as x = x - 2
console.log("x -= 2 ->", x); // 13

x *= 2;  // Same as x = x * 2
console.log("x *= 2 ->", x); // 26
        
Output will appear here...

4. Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

let num = 5;
let str = "5";

console.log(num == str);  // true (values match)
console.log(num === str); // false (types differ: number vs string)
console.log(num != 8);    // true
console.log(num > 2);     // true
        
Output will appear here...

5. Logical Operators

Logical operators are used to determine the logic between variables or values.

let x = 6;
let y = 3;

console.log(x < 10 && y > 1); // true (both true)
console.log(x == 5 || y == 5); // false (neither is true)
console.log(!(x == y));       // true (inverse of false)
        
Output will appear here...

6. Ternary Operator

A shorthand for the if...else statement.

Syntax

condition ? value_if_true : value_if_false

Example

let age = 20;
let type = (age >= 18) ? "Adult" : "Minor";
console.log(type);
        
Output will appear here...

7. Typeof Operator

The typeof operator returns the type of a variable or an expression.

console.log(typeof "John"); // string
console.log(typeof 3.14);   // number
console.log(typeof true);   // boolean
console.log(typeof [1,2]);  // object (arrays are objects)
console.log(typeof {x:1});  // object
        
Output will appear here...

8. Comparison Table: == vs ===

Feature == (Loose Equality) === (Strict Equality)
Compares Only Values Values AND Data Types
Type Coercion Yes (e.g., "10" becomes 10) No
Example 10 == "10" 10 === "10"
Result true false

Practice Questions

Test your understanding of JavaScript Operators.

Easy

Q1: Take marks of 5 subjects, Find total and percentage.

Easy

Q2: Calculate total salary given the following:

  • Basic salary = 25000
  • HRA = 15% of basic
  • DA = 8% of basic
Medium

Q3: Calculate Simple Interest.

  • P (Principal) = 10000
  • R (Rate) = 7%
  • T (Time) = 2 years
Medium

Q4: Find the Area of a Rectangle.

  • Length = 12
  • Breadth = 8
Hard

Q5: Write a program to calculate the final salary step-by-step.

  • Basic Salary = 40000
  • HRA = 25% of basic
  • DA = 12% of basic
  • Bonus = 10% of (basic + HRA + DA)
  • Tax = 5% of total salary (after adding bonus)

👉 Find: Gross Salary (basic + HRA + DA), Total Salary (after bonus), and Final Salary (after tax deduction).

Answer

Code: