Loops and Codistion Statments
Loops and Codistion Statments
The === operator in JavaScript is the strict equality operator. It compares both
value and data type without type conversion.
5. Summary
Operator Behavior Use Case
Converts data types before Use with caution, may cause
==
comparing unexpected results
Recommended for accurate
=== Checks both value & type
comparisons
Loops
1. Types of Loops
Loop Type Syntax Structure When to Use
for(initialization; condition; When the number of iterations is
For Loop
increment/decrement) { code } known.
When the number of iterations is
While Loop while(condition) { code } unknown but depends on a
condition.
When the loop must execute at least
Do...While Loop do { code } while(condition);
once before checking the condition.
✅ Best for:
Waiting for user input (while(userInput !== "exit")).
Running loops until a certain condition is met dynamically.
Syntax:
do {
// Code to execute
} while (condition);
Example: Run at least once
let num = 10;
do {
console.log("Executed once, even if condition is false");
} while (num < 5);
🔹 Key Difference from while loop:
do...while executes first, then checks the condition.
Good for prompts, user inputs, and menus where execution must happen
at least once.
✅ Best for:
Ensuring at least one execution of a block.
Prompting users for input (do { prompt() } while(inputInvalid)).
53,38,20,61,57,31,54,34,23,24,12,18,56,14,33
Conditional Statements in JavaScript
Conditional statements allow us to control the flow of a program by executing
different code blocks based on conditions.
switch (day) {
case "Monday":
console.log("Start of the week!");
break;
case "Friday":
console.log("Weekend is near!");
break;
case "Sunday":
console.log("It's a holiday!");
break;
default:
console.log("It's a regular day.");
}
🔹 How it works?
1. The switch statement compares day with each case.
2. If day === "Monday", it prints "Start of the week!".
3. The break; statement prevents execution of the next cases.
4. If no match is found, the default case executes.
✅ Best for:
Handling multiple values of a single variable (e.g., menu options, enums).
When comparisons are based on exact values (not conditions like >, <).
🚨 Important:
If break; is omitted, execution "falls through" to the next case!
switch (fruit) {
case "Apple":
console.log("It's an apple!");
case "Mango":
console.log("It's a mango!");
default:
console.log("Unknown fruit.");
}
Output:
It's an apple!
It's a mango!
Unknown fruit.
🔹 Fix: Add break; after each case.
switch (accountType) {
case "Savings":
if (balance > 1000) {
console.log("You have a good balance in your savings account.");
} else {
console.log("Your savings balance is low.");
}
break;
case "Current":
console.log("This is a business account.");
break;
default:
console.log("Invalid account type.");
}
✅ Best for:
Handling hierarchical decision-making.
When switch handles primary conditions and if...else handles sub-
conditions.
5. Ternary Operator (? :) – Shorter Conditional Statements
A compact way to write simple if...else conditions.
Syntax:
condition ? "if true" : "if false";
Example:
let age = 18;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Output: "Adult"
✅ Best for:
Quick conditional assignments.
Making code concise and readable.
1. What is an Event?
An event is any user action, such as clicking a button, pressing a key, or moving
the mouse. JavaScript allows to listen for these events and execute code
accordingly.
Common Events in JavaScript
Event Type Description
click Triggered when an element is clicked
mouseover Triggered when the mouse hovers over an element
mouseout Triggered when the mouse leaves an element
keydown Triggered when a key is pressed
keyup Triggered when a key is released
change Triggered when the value of an input element changes
submit Triggered when a form is submitted
load Triggered when the webpage finishes loading