JavaScript Lecture: Loops, Conditions, and Functions
Introduction
JavaScript is a programming language used to make web pages interactive. In this lecture,
we will learn about loops, conditional statements, and functions.
Loops in JavaScript
Loops are used to execute a block of code repeatedly.
For Loop Example:
for (let i = 1; i <= 5; i++) {
[Link]("Number: " + i);
While Loop Example:
let i = 1;
while (i <= 5) {
[Link]("Count: " + i);
i++;
Do While Loop Example:
let i = 1;
do {
[Link]("Value: " + i);
i++;
} while (i <= 5);
Conditional Statements
If Statement:
let age = 18;
if (age >= 18) {
[Link]("You are eligible to vote");
If Else Statement:
let number = 5;
if (number % 2 === 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
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");
Functions in JavaScript
Greet Function Example:
function greet(name) {
[Link]("Hello, " + name + "! Welcome to JavaScript.");
greet("Ali");
greet("Sara");
Summary
Loops repeat code, conditions make decisions, and functions organize code.