JavaScript
Operators



What are Operators in JavaScript?

Operators in JavaScript are special symbols used to perform operations on operands (values or variables). Think of them as tools that help you calculate, compare, assign, or manipulate data in your program.

Types of JavaScript Operators

Arithmetic Operators

These operators perform basic mathematical operations:

Example:


let a = 10;
let b = 3;

console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Modulus:", a % b);
console.log("Exponentiation:", a ** b);
    

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Exponentiation: 1000
    

Assignment Operators

These assign values to variables. You can also use them to perform operations and assign the result in one step:

Example:


let x = 5;
x += 3; // x = x + 3
console.log("After += 3:", x);
x *= 2; // x = x * 2
console.log("After *= 2:", x);
    

Output:

After += 3: 8
After *= 2: 16
    

Comparison Operators

Used to compare two values. Returns a Boolean value (true/false):

Example:


console.log(5 == "5");    // true (type coercion)
console.log(5 === "5");   // false (different types)
console.log(10 > 7);      // true
console.log(3 <= 2);      // false
    

Output:

true
false
true
false
    

Logical Operators

Used to combine multiple conditions:

Example:


let isLoggedIn = true;
let hasPermission = false;

console.log("Can access?", isLoggedIn && hasPermission);
console.log("Partial access?", isLoggedIn || hasPermission);
console.log("Not logged in?", !isLoggedIn);
    

Output:

Can access? false
Partial access? true
Not logged in? false
    

Unary Operators

Works with only one operand.

Example:


let y = "5";
console.log(typeof +y);  // number
let count = 1;
console.log(++count);    // 2
    

Output:

number
2
    

Ternary Operator

This is a shortcut for if-else condition. Syntax: condition ? valueIfTrue : valueIfFalse

Example:


let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log("Status:", status);
    

Output:

Status: Adult
    

Type Operators

Example:


let text = "Hello";
console.log(typeof text); // string

let today = new Date();
console.log(today instanceof Date); // true
    

Output:

string
true
    

Beginner Tip

Q: What’s the difference between == and === in JavaScript?

A: == compares only values (performs type coercion), while === compares both value and type. Always prefer === to avoid unexpected bugs.

Q: Why use the ternary operator when we already have if-else?

A: Ternary operator is useful for quick decisions or assignments. It makes the code concise but use it wisely to maintain readability.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M