JavaScript Conditional and Looping Control
Instructions
This document provides a complete and in-depth explanation of JavaScript conditional and
looping control instructions. Each concept includes its definition, syntax, explanation, program
examples, uses, advantages, and disadvantages. It is written in a continuous textbook-style
format for deeper understanding.
1. Conditional Control Instructions
Conditional control instructions are used to make decisions in JavaScript programs. These
statements allow certain parts of the code to be executed only when a specified condition is
true. If the condition is false, the control is transferred to another part of the code. JavaScript
provides several conditional control statements such as if, if...else, if...else if ladder, switch,
and ternary operators.
1.1 The if Statement
The simplest form of decision-making structure is the if statement. It evaluates a condition and
executes a block of code only if the condition is true.
Syntax:
if (condition) { // code to execute if condition is true }
Example Program:
let age = 20; if (age >= 18) { [Link]("You are eligible to vote."); }
Use: Used when you need to execute a single block of code only when a specific condition is
satisfied.
Advantages: Easy to implement, simple structure.
Disadvantages: Cannot handle multiple conditions efficiently.
1.2 The if...else Statement
This statement allows two possible paths of execution — one if the condition is true and
another if it is false.
Syntax:
if (condition) { // executes when condition is true } else { // executes when condition is false }
Example Program:
let num = 10; if (num % 2 === 0) { [Link]("Even number"); } else { [Link]("Odd
number"); }
Use: Used when there are two alternative actions based on the result of the condition.
Advantages: Makes the decision process clear and structured.
Disadvantages: Not efficient for multiple decisions.
1.3 The if...else if...else Ladder
The if...else if...else ladder is used when multiple conditions need to be checked in sequence.
The program executes the first block whose condition is true.
Example Program:
let marks = 75; if (marks >= 90) { [Link]("Grade A"); } else if (marks >= 60) {
[Link]("Grade B"); } else { [Link]("Grade C"); }
Use: Used when more than two conditions must be evaluated.
Advantages: Allows multiple conditions in sequence.
Disadvantages: Becomes lengthy and difficult to read for many conditions.
1.4 The switch Statement
The switch statement is used when a variable or expression is compared with several constant
values. It provides a clean and readable alternative to long if...else if ladders.
Example Program:
let day = 4; switch(day) { case 1: [Link]("Monday"); break; case 2:
[Link]("Tuesday"); break; case 3: [Link]("Wednesday"); break; case 4:
[Link]("Thursday"); break; default: [Link]("Invalid day"); }
Use: Used when one variable is compared with several fixed values.
Advantages: Improves code readability and maintenance.
Disadvantages: Cannot handle relational or complex logical conditions.
1.5 The Conditional (Ternary) Operator (?:)
The ternary operator is a shorthand way of writing simple if...else statements. It returns one of
two values depending on the result of a condition.
Syntax:
condition ? value_if_true : value_if_false;
Example Program:
let age = 17; let result = (age >= 18) ? "Adult" : "Minor"; [Link](result);
Use: Used for simple conditional assignments.
Advantages: Compact and improves readability for short conditions.
Disadvantages: Not suitable for complex conditions.
2. Looping Control Instructions
Looping statements in JavaScript are used to execute a block of code repeatedly until a
specific condition becomes false. These structures help to reduce redundancy and make
programs efficient. There are several types of loops such as for, while, do...while, for...in, and
for...of loops.
2.1 The for Loop
The for loop is used when the number of iterations is known. It contains three parts:
initialization, condition, and increment/decrement.
Example Program:
for (let i = 1; i <= 5; i++) { [Link]("Number: " + i); }
Use: Ideal for fixed iteration counts.
Advantages: Compact and easy to control.
Disadvantages: Not flexible for unknown iteration counts.
2.2 The while Loop
The while loop executes a block of code repeatedly as long as the condition evaluates to true.
It is useful when the number of iterations is not predetermined.
Example Program:
let i = 1; while (i <= 5) { [Link]("Count: " + i); i++; }
Use: Used when the loop termination condition is dynamic.
Advantages: Useful for unpredictable repetition.
Disadvantages: Risk of infinite loop if the condition never becomes false.
2.3 The do...while Loop
The do...while loop executes the block of code once before checking the condition. This
ensures that the loop body executes at least one time.
Example Program:
let i = 1; do { [Link]("Iteration: " + i); i++; } while (i <= 5);
Use: Used when the loop must run at least once regardless of condition.
Advantages: Guarantees one-time execution.
Disadvantages: May execute unwanted code once if condition is false initially.
2.4 The for...in Loop
The for...in loop is designed to iterate over the enumerable properties of an object. It returns
keys (property names) of an object.
Example Program:
let person = {name: "John", age: 25, city: "Delhi"}; for (let key in person) { [Link](key + ": "
+ person[key]); }
Use: Used to loop through object properties.
Advantages: Simplifies iteration through object keys.
Disadvantages: Not suitable for arrays because order is not guaranteed.
2.5 The for...of Loop
The for...of loop is used to iterate over iterable objects such as arrays, strings, and sets. It
retrieves values directly rather than keys or indexes.
Example Program:
let colors = ["red", "green", "blue"]; for (let color of colors) { [Link](color); }
Use: Ideal for looping through arrays or strings.
Advantages: Clean and concise for value iteration.
Disadvantages: Does not give direct access to index or key.
Summary
Conditional statements help make decisions in a program based on specific conditions, while
looping statements help to repeat code efficiently. Understanding when to use which type is
fundamental to writing effective and logical JavaScript programs.