«Back to Home

Learn JavaScript

Topics

Operators in JavaScript

Operators are symbols that perform actions on values.
Every line of JavaScript you write will likely use some operator—whether you’re calculating values, comparing numbers, checking conditions, or updating variables.

Understanding operators is important for college students and freshers because they form the foundation of programming logic.

In this chapter, you will learn:

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • Logical Operators

  • Unary Operators

  • Ternary Operator

Let’s learn each of them with simple explanations and examples.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remainder (modulus)a % b
**Power / Exponenta ** b

Example:

let a = 10;
let b = 3;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);

Output:

137303.333333333333333511000

2. Assignment Operators

These assign values to variables.

OperatorMeaningExample
=Assigna = 5
+=Add and assigna += 3 (a = a + 3)
-=Subtract and assigna -= 2
*=Multiply and assigna *= 2
/=Divide and assigna /= 2

Example:

let x = 10;

x += 5;
console.log(x);

x -= 3;
console.log(x);

x *= 2;
console.log(x);

Output:

15
12
24

3. Comparison Operators

Used to compare two values.
They return true or false.

OperatorMeaning
==Equal (value only)
===Strict equal (value + type)
!=Not equal
!==Strict not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Example:

console.log(5 == "5");
console.log(5 === "5");
console.log(10 > 5);
console.log(10 <= 10);

Output:

truefalsetruetrue

Important:
Use === instead of == to avoid unexpected results.

4. Logical Operators

Used in conditions.

OperatorMeaning
&&AND
||OR
!NOT

Example:

let age = 20;

console.log(age > 18 && age < 30);
console.log(age < 18 || age > 30);
console.log(!true);

Output:

truefalsefalse

5. Unary Operators

Unary operators work with a single value.

Increment (++) and Decrement (--)

let n = 5;

n++;
console.log(n);

n--;
console.log(n);

Output:

6
5

typeof operator

Used to check the type of data.

console.log(typeof 10);
console.log(typeof "Hello");
console.log(typeof true);
console.log(typeof []);

Output:

numberstringbooleanobject

Arrays show as “object” (JavaScript design behavior).

6. Ternary Operator (Short if-else)

The ternary operator provides a compact way to write conditions.

Syntax:

condition ? valueIfTrue : valueIfFalse

Example:

let age = 18;

let result = age >= 18 ? "Eligible" : "Not Eligible";

console.log(result);

Output:

Eligible

Real-Life Example: Pass/Fail Check

let marks = 40;

let status = marks >= 35 ? "Pass" : "Fail";

console.log(status);

Output:

Pass

Real-Life Example: Discount Calculation

let price = 500;

let discount = price > 300 ? 50 : 20;

console.log("Discount:", discount);

Output:

Discount: 50

Example Program (All Operators Combined)

let a = 10;
let b = 5;

console.log(a + b);
console.log(a > b);
console.log(a === 10);

let msg = (a % 2 === 0) ? "Even" : "Odd";
console.log(msg);

a += 20;
console.log(a);

Output:

15truetrue
Even
30

Common Mistakes Beginners Make

  1. Using == instead of ===

  2. Using assignment (=) instead of comparison (== or ===)

  3. Forgetting that ! changes true ? false

  4. Confusing && with ||

  5. Misusing increment and decrement operators