Track: JavaScript Basics
Lesson: JS-02 / JS-10
🎯 Lesson Goal
By the end of this lesson, you will understand different types of operators in JavaScript and how expressions are formed to perform calculations, comparisons, and logical decisions in programs.
📌 Prerequisites
- Completion of JS-01: Variables and Data Types
- Basic understanding of variables and values
📘 What are Operators?
An operator is a symbol used to perform an operation on one or more operands (values or variables).
Example:
let sum = 10 + 5;
Here:
+is the operator10and5are operands10 + 5is an expression
📘 What is an Expression?
An expression is a combination of variables, values, and operators that produces a result.
5 + 3 // Expression
age >= 18 // Expression
x * y + z // Expression
🔢 Arithmetic Operators
Used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 10 + 5 |
- | Subtraction | 10 - 5 |
* | Multiplication | 10 * 5 |
/ | Division | 10 / 5 |
% | Modulus (remainder) | 10 % 3 |
Example:
let a = 20;
let b = 6;
console.log(a + b); // 26
console.log(a % b); // 2
🔁 Assignment Operators
Used to assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 10 | x = 10 |
+= | x += 5 | x = x + 5 |
-= | x -= 2 | x = x - 2 |
*= | x *= 3 | x = x * 3 |
Example:
let score = 50;
score += 10;
console.log(score); // 60
🔍 Comparison Operators
Used to compare two values. The result is true or false.
| Operator | Meaning |
|---|---|
== | Equal to (value only) |
=== | Strict equal (value + type) |
!= | Not equal |
!== | Strict not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example:
console.log(10 == "10"); // true
console.log(10 === "10"); // false
✔ Always prefer === in modern JavaScript.
🧠 Logical Operators
Used to combine multiple conditions.
| Operator | Meaning |
|---|---|
&& | AND |
| ` | |
! | NOT |
Example:
let age = 22;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Access granted");
}
➕ Unary Operators
Operate on a single operand.
| Operator | Example | Description |
|---|---|---|
++ | x++ | Increment |
-- | x-- | Decrement |
! | !true | Logical NOT |
Example:
let count = 5;
count++;
console.log(count); // 6
📐 Operator Precedence
JavaScript follows BODMAS/PEMDAS rules.
Example:
let result = 10 + 5 * 2;
console.log(result); // 20
To control order, use parentheses:
let result = (10 + 5) * 2;
console.log(result); // 30
⚠️ Common Mistakes
- Using
==instead of=== - Forgetting operator precedence
- Misusing logical operators
- Confusing assignment
=with comparison==
🧠 Practice Tasks
- Write an expression that adds 3 numbers
- Check if a number is greater than 50
- Use
===to compare a number and string - Write a condition using
&& - Predict the output of:
5 + 2 * 3
📝 Summary
- Operators perform operations on values
- Expressions always produce a result
- Use
===for safe comparisons - Logical operators help make decisions
- Parentheses control evaluation order
➡️ Next Lesson
JavaScript Conditional Statements (JS-03)
🏅 Quiz & Badge
Quiz and badges will be enabled soon.