Chapter: Selection Statement in JavaScript
— Switch Case
Sometimes a program must choose one action from many possible options. Using many
if…else if statements makes the code long and confusing.
To make this easier and clearer, JavaScript provides the switch statement, which checks one
variable or expression against many possible values.
How the switch Statement Works
1. The expression inside the switch is evaluated once.
2. Its result is compared with each case value.
3. When a matching case is found, the statements inside that case execute.
4. The break statement stops execution so that other cases do not run.
5. If no case matches, the default block runs.
General Syntax
switch(expression) {
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Role of break Statement
The break statement stops further case checking once a match is found.
If break is not used:
Execution continues into the next case.
This behavior is called fall-through.
It may cause unwanted results.
Chapter: Iteration in JavaScript (Loops)
Loops are used when we want to repeat a block of code multiple times automatically
instead of writing the same code again and again.
Loops save time and make programs shorter and easier to maintain.
Types of Loops in JavaScript
JavaScript mainly provides four loops:
for loop
while loop
do...while loop
for...in loop
The for Loop
A for loop is used when the number of repetitions is already known.
Parts of a for Loop
A for loop has three important parts:
1. Initialization – starting value of loop variable.
2. Condition – loop runs while this is true.
3. Update – changes variable value after each iteration.
Syntax
for(initialization; condition; update) {
statements;
}
Flow of Execution
1. Initialization runs once.
2. Condition is checked.
3. Loop body executes if condition is true.
4. Update changes variable value.
5. Condition is checked again.
6. Process repeats until condition becomes false.
Control Variable
The variable controlling the loop (like i or a) is called the control variable.
Its value decides how long the loop runs.
The while Loop
A while loop repeats code as long as a condition is true.
Flow
1. Condition is checked first.
2. If true, loop body runs.
3. Variable must be updated inside the loop.
4. Condition is checked again.
5. Loop stops when condition becomes false.
Important Note
If the control variable is not updated, the condition never becomes false, causing an infinite
loop.
The do...while Loop
In this loop, code runs first and condition is checked afterward.
Therefore, the loop runs at least once, even if the condition is false.
Difference Between while and do...while
while Loop
Condition checked first.
Loop may run zero times.
Entry-controlled loop.
do...while Loop
Code runs first.
Loop runs at least once.
Exit-controlled loop.
Break and Continue Statements
break Statement
Stops the loop immediately and moves execution outside the loop.
continue Statement
Skips current iteration and jumps to the next loop cycle.
For...in Loop
The for...in loop is used to access properties of an object one by one.
Example:
for (var key in object) {
[Link](key);
}
JavaScript Functions
A function is a block of reusable code designed to perform a particular task.
Functions help:
Avoid repetition
Organize programs
Improve readability
Function Syntax
function functionName(parameters) {
statements;
}
A function runs when it is called.
Return Statement
The return statement:
Sends a value back from the function.
Stops further execution of the function.
Example:
function multiply(a, b) {
return a * b;
}
Local and Global Variables
Local Variable
Exists only inside a function and cannot be accessed outside.
Global Variable
Declared outside functions and accessible everywhere.
Fibonacci Series
The Fibonacci series is a sequence where:
First two numbers are usually 0 and 1.
Each next number is the sum of the previous two.
Example: 0, 1, 1, 2, 3, 5, 8, 13...
Program Example
var a = 0, b = 1, c, i;
[Link](a + "<br>");
[Link](b + "<br>");
for(i = 2; i < 10; i++) {
c = a + b;
[Link](c + "<br>");
a = b;
b = c;
}
Events in JavaScript
An event occurs when the user interacts with a webpage.
Common Events
Clicking a button
Pressing a key
Loading a page
Moving mouse
Changing input value
Event Handler
A function that runs when an event occurs.
Event Attributes
Common event attributes:
onclick
onchange
onmouseover
onmouseout
onload
onkeydown
Inline Event Handling
Events can be handled directly in HTML elements.
Example:
<button onclick="alert('Hello')">Click</button>
Introduction to E-Commerce
E-commerce means buying and selling goods or services online using the internet.
Technologies Used
Internet services
Digital payment systems
Mobile commerce apps
Online data exchange
Types of E-Commerce
B2B
Business sells to another business.
B2C
Business sells to customers.
C2C
Customers sell to other customers.
Advantages of E-Commerce
For Organizations
Global market access
Reduced operating cost
Faster services
For Customers
Shop anytime
More choices
Easy price comparison
For Society
Reduced travel
Less pollution
Better access to services
Disadvantages of E-Commerce
Security risks
Delivery delays
Cannot inspect products physically
Electronic Banking (E-Banking)
E-banking allows customers to perform banking activities electronically.
Examples
ATM services
Debit cards
Credit cards
Internet banking
Mobile banking
Advantages of E-Banking
Saves time
Available anytime
Quick transfers
Lower costs
Online Safety Precautions
1. Share minimal personal information.
2. Verify website security.
3. Keep payment records.
4. Change passwords regularly.
5. Use secure payment methods.