JavaScript

JavaScript Operators and Expressions (JS-02)

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 operator
  • 10 and 5 are operands
  • 10 + 5 is 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.

OperatorDescriptionExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5
/Division10 / 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.

OperatorExampleEquivalent To
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 2x = x - 2
*=x *= 3x = 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.

OperatorMeaning
==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.

OperatorMeaning
&&AND
`
!NOT

Example:

let age = 22;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("Access granted");
}

➕ Unary Operators

Operate on a single operand.

OperatorExampleDescription
++x++Increment
--x--Decrement
!!trueLogical 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

  1. Write an expression that adds 3 numbers
  2. Check if a number is greater than 50
  3. Use === to compare a number and string
  4. Write a condition using &&
  5. 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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *