Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

4.5 Operators

Updated Jul 23, 2026

4.5 Operators

Updated Jul 23, 2026

4.5 Operators

Operators are symbols that perform operations on values and variables.

Example:

let sum = 10 + 5;

Here:

  • 10 and 5 → Operands
  • + → Operator

Output:

15

Operators allow JavaScript to perform calculations, compare values, assign data, make decisions, and manipulate variables.


What are Operators?

Operators allow JavaScript to:

  • Perform Calculations
  • Compare Values
  • Assign Data
  • Make Decisions
  • Manipulate Bits
  • Work with Strings

Types of Operators

JavaScript provides several categories of operators:

  1. Assignment Operators
  2. Arithmetic Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Unary Operators
  7. Conditional (Ternary) Operator
  8. String Operators

Assignment Operators

Assignment operators assign values to variables.

Basic Assignment (=)

let age = 21;

Meaning:

Store 21 inside age

Example

let x = 10;

console.log(x);

Output

10

Addition Assignment (+=)

let x = 10;

x += 5;

Equivalent to

x = x + 5;

Output

15

Subtraction Assignment (-=)

let x = 10;

x -= 3;

Equivalent to

x = x - 3;

Output

7

Multiplication Assignment (*=)

let x = 10;

x *= 2;

Output

20

Division Assignment (/=)

let x = 10;

x /= 2;

Output

5

Modulus Assignment (%=)

let x = 10;

x %= 3;

Output

1

Assignment Operators Summary

Operator Example Equivalent
= x = 5 Assign value
+= x += 2 x = x + 2
-= x -= 2 x = x - 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
%= x %= 2 x = x % 2

Arithmetic Operators

Arithmetic operators perform mathematical calculations.

Addition (+)

10 + 5;

Output

15

Subtraction (-)

10 - 5;

Output

5

Multiplication (*)

10 * 5;

Output

50

Division (/)

10 / 5;

Output

2

Modulus (%)

Returns the remainder after division.

10 % 3;

Output

1

Exponentiation (**)

Raises a number to a power.

2 ** 3;

Output

8

Equivalent to

2 × 2 × 2

Arithmetic Operators Summary

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation

Comparison Operators

Comparison operators compare two values.

The result is always:

  • true
  • false

Equal (==)

5 == "5";

Output

true

JavaScript converts the values before comparing them.


Strict Equal (===)

5 === "5";

Output

false

No type conversion occurs.

Best Practice: Always prefer === over ==.


Not Equal (!=)

5 != 3;

Output

true

Strict Not Equal (!==)

5 !== "5";

Output

true

Greater Than (>)

10 > 5;

Output

true

Less Than (<)

5 < 10;

Output

true

Greater Than or Equal (>=)

10 >= 10;

Output

true

Less Than or Equal (<=)

5 <= 10;

Output

true

Comparison Operators Summary

Operator Meaning
== Equal
=== Strict Equal
!= Not Equal
!== Strict Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal

Logical Operators

Logical operators combine multiple conditions.


AND (&&)

Returns true only if both conditions are true.

true && true;

Output

true

Example

age >= 18 && age <= 60;

Meaning:

Age Between 18 and 60

OR (||)

Returns true if at least one condition is true.

true || false;

Output

true

Example

role === "admin" || role === "manager";

NOT (!)

Reverses a boolean value.

!true;

Output

false

Example

let isLoggedIn = false;

console.log(!isLoggedIn);

Output

true

Logical Operators Summary

Operator Meaning
&& AND
|| OR
! NOT

Bitwise Operators

Bitwise operators work directly with binary numbers.

These are mainly used in low-level programming and are less common in everyday web development.


AND (&)

5 & 1;

Binary

5 = 101
1 = 001
--------
    001

Output

1

OR (|)

5 | 1;

Output

5

XOR (^)

5 ^ 1;

Output

4

NOT (~)

~5;

Output

-6

Left Shift (<<)

5 << 1;

Output

10

Right Shift (>>)

10 >> 1;

Output

5

Bitwise Operators Summary

Operator Meaning
& AND
| OR
^ XOR
~ NOT
<< Left Shift
>> Right Shift

Unary Operators

Unary operators work with a single operand.


Unary Plus (+)

Converts a value into a number.

+"100";

Output

100

Unary Minus (-)

-"100";

Output

-100

Increment (++)

Adds one.

let x = 5;

x++;

Output

6

Decrement (--)

Subtracts one.

let x = 5;

x--;

Output

4

typeof

Returns the data type.

typeof "Hello";

Output

string

delete

Removes an object property.

const user = {

    name: "Samir"

};

delete user.name;

Unary Operators Summary

Operator Meaning
+ Convert to Number
- Negative Value
++ Increment
-- Decrement
typeof Data Type
delete Remove Property

Conditional (Ternary) Operator

The ternary operator is a shorter way of writing an if...else statement.

Syntax

condition
? valueIfTrue
: valueIfFalse;

Example

let age = 20;

let result =
age >= 18
? "Adult"
: "Minor";

Output

Adult

Equivalent if...else

if (age >= 18) {

    result = "Adult";

} else {

    result = "Minor";

}

Another Example

const isLoggedIn = true;

const message =
isLoggedIn
? "Welcome"
: "Login First";

Output

Welcome

String Operators

String operators combine and manipulate text.


Concatenation (+)

"Hello" + " World";

Output

Hello World

Example

let firstName = "Samir";
let lastName = "Niroula";

let fullName =
firstName + " " + lastName;

Output

Samir Niroula

Concatenation Assignment (+=)

let text = "Hello";

text += " World";

Output

Hello World

Template Literals (Recommended)

Instead of

let fullName =
firstName + " " + lastName;

Use

let fullName =
`${firstName} ${lastName}`;

Template literals are cleaner and easier to read.


Complete Example

let age = 21;

age += 1;

let status =
age >= 18
? "Adult"
: "Minor";

console.log(status);

Output

Adult

Operator Precedence

JavaScript follows mathematical precedence rules.

Example

10 + 5 * 2;

Output

20

Because:

5 × 2 = 10

10 + 10 = 20

Parentheses

Parentheses change the order of evaluation.

(10 + 5) * 2;

Output

30

Summary

Category Operators
Assignment =, +=, -=, *=, /=, %=
Arithmetic +, -, *, /, %, **
Comparison ==, ===, !=, !==, >, <, >=, <=
Logical &&, ||, !
Bitwise &, |, ^, ~, <<, >>
Unary +, -, ++, --, typeof, delete
Conditional ?:
String +, +=

Key Takeaway

Operators allow JavaScript to perform calculations, comparisons, assignments, and logical decisions.

The most commonly used operators in modern JavaScript are:

+
-
*
/
===
!==
>
<
&&
||
++
--
?:

Remember:

Assignment  → Store Values
Arithmetic  → Perform Math
Comparison  → Compare Values
Logical     → Combine Conditions
Unary       → Modify Single Values
Ternary     → Short if...else
String      → Join Text

Learning these operators is essential because they are used in almost every JavaScript program.

4.4 Type Conversion4.6 Control Flow