JavaScript Lecture Notes
Loops, Conditional Statements, and Functions
1. Introduction
JavaScript is a widely used programming language for creating dynamic and interactive web
pages. This lecture covers fundamental programming concepts including loops, conditional
statements, and functions with clear explanations and examples.
2. Loops in JavaScript
Loops are used to repeatedly execute a block of code while a specific condition remains
true.
2.1 For Loop
The for loop is used when the number of iterations is known in advance.
for (let i = 1; i <= 5; i++) {
[Link]("Number: " + i);
}
2.2 While Loop
The while loop continues execution as long as the condition is true.
let i = 1;
while (i <= 5) {
[Link]("Count: " + i);
i++;
}
2.3 Do While Loop
The do...while loop ensures the code runs at least once.
let i = 1;
do {
[Link]("Value: " + i);
i++;
} while (i <= 5);
3. Conditional Statements
Conditional statements allow programs to make decisions based on different conditions.
3.1 If Statement
let age = 18;
if (age >= 18) {
[Link]("You are eligible to vote");
}
3.2 If Else Statement
let number = 5;
if (number % 2 === 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
3.3 If Else If Else Statement
let marks = 75;
if (marks >= 80) {
[Link]("Grade A");
} else if (marks >= 60) {
[Link]("Grade B");
} else if (marks >= 40) {
[Link]("Grade C");
} else {
[Link]("Fail");
}
4. Functions in JavaScript
Functions are reusable blocks of code designed to perform a specific task.
4.1 Greet Function Example
function greet(name) {
[Link]("Hello, " + name + "! Welcome to JavaScript.");
}
greet("Ali");
greet("Sara");
5. Summary
• Loops execute repetitive tasks efficiently.
• Conditional statements control logical flow.
• Functions improve code organization and reusability.