JavaScript Nested Logic
- This lesson explains nested conditional and looping logic in JavaScript.
Introduction to Nested Logic
In real-world programming, decisions and repetitions are rarely simple.
Often, one condition depends on another condition, or one loop needs to run inside another loop.This concept is called Nested Logic.
What Does “Nested” Mean ?
Nested means:
One statement placed inside another statement
Examples:
An if inside another if
A loop inside another loop
A condition inside a loop
Why Do We Need Nested Logic ?
Nested logic is used when:
Decisions depend on previous decisions
Data is multi-level (rows and columns)
Patterns need to be created
Complex validation is required
Without nested logic:
Code becomes incorrect
Logic becomes incomplete
Real-world problems cannot be solved properly
Nested Conditions
What Are Nested Conditions ?
A nested condition means:
Writing one conditional statement inside another conditional statement
Usually done using:
if inside if
if-else inside if or else
Basic Syntax of Nested if
if (condition1) {
if (condition2) {
// code
}
}
How Nested Conditions Work
Outer condition is checked first
If outer condition is true, inner condition is checked
Nested Conditions
Handles multiple dependent conditions by placing one if statement inside another.
// Nested if example
let age = 20;
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log("Entry allowed");
}
}
Explanation
First checks age
Then checks ID
Entry allowed only if both conditions are satisfied
Nested if-else Structure
if (condition1) {
if (condition2) {
// code A
} else {
// code B
}
} else {
// code C
}
Nested Login Validation
Verifies username first, then checks password inside the condition.
// Basic login system using nested if
let username = "admin";
let password = "1234";
if (username === "admin") {
if (password === "1234") {
console.log("Login successful");
} else {
console.log("Wrong password");
}
} else {
console.log("User not found");
}
When to Use Nested Conditions ?
Multi-step validation
Role-based access
Grading systems
Menu-based programs
Common Mistakes in Nested Conditions
Too many nested levels
Missing braces { }
Confusing indentation
Using nested if instead of logical operators
Best Practices for Nested Conditions
Keep nesting shallow
Use proper indentation
Comment complex logic
Avoid unnecessary nesting
Nested Loops
What Are Nested Loops ?
A nested loop means:
A loop placed inside another loop
The inner loop runs completely for every single iteration of the outer loop.
Basic Syntax of Nested Loops
for (outer) {
for (inner) {
// code
}
}
How Nested Loops Work
Outer loop starts
Inner loop runs fully
Outer loop moves to next iteration
Inner loop runs again
Nested Loops
Runs one loop inside another to handle multi-level iterations and patterns.
// Simple nested loop
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 2; j++) {
console.log("i =", i, "j =", j);
}
}
// Multiplication table
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 10; j++) {
console.log(i + " x " + j + " = " + (i * j));
}
console.log("----------");
}
// Pattern printing
for (let i = 1; i <= 4; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += "* ";
}
console.log(row);
}
// Nested while loop example
let i = 1;
while (i <= 3) {
let j = 1;
while (j <= 2) {
console.log("i =", i, "j =", j);
j++;
}
i++;
}
When to Use Nested Loops ?
Tables and matrices
Pattern problems
Multi-dimensional data
Comparing multiple values
Performance Warning
Nested loops increase execution time.
Example:
Outer loop runs 10 times
Inner loop runs 10 times
Total executions = 100
Avoid unnecessary nesting for large data.
Common Beginner Mistakes in Nested Loops
Infinite loops
Wrong loop condition
Forgetting to reset inner loop variable
Confusing outer and inner counters
Best Practices for Nested Loops
Use meaningful variable names
Keep loops simple
Avoid deep nesting
Break logic into functions if needed
Nested Conditions vs Nested Loops
Feature Nested Conditions Nested Loops Purpose Decision making Repetition Structure if inside if loop inside loop Use case Validation Data processing Complexity Logical Iterative