Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes 4310702

Rating
-
Sold
-
Pages
6
Uploaded on
01-03-2025
Written in
2024/2025

Lecture notes of 6 pages for the course 4310702 at UoN (Javascript)

Institution
Course

Content preview

Loops
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions
typically return true or false. A loop will continue running until the defined condition returns
false.


Types of Loops

JavaScript provides the following types of loops:

1.​ for loop – Used when the number of iterations is known.
2.​ while loop – Repeats while a condition is true.
3.​ do...while loop – Executes at least once, then repeats while a condition is true.
4.​ for...in loop – Iterates over the properties of an object.
5.​ for...of loop – Iterates over iterable objects like arrays and strings.

For Loop
The for loop is used when we know how many times we need to run a block of code.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute
}


Initialization → Runs once before the loop starts.
Condition → If true, the loop runs; if false, the loop stops.
Increment/Decrement → Updates the loop variable.


Example: Print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}


Example: Loop through an Array

, let fruits = ["Apple", "Banana", "Cherry"];


for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}


While Loop

The while loop starts by evaluating condition. If condition evaluates to true, the code in the code
block gets executed. If condition evaluates to false, the code in the code block is not executed
and the loop ends.

Syntax
while (condition) {
// Code to execute
}


Example: Print numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log(i);
i++; // Increment
}


do...while Loop

The do...while loop is closely related to while loop. In a do...while loop, condition is checked at
the end of each iteration of the loop, rather than at the beginning before the loop runs. This
means that code in a do...while loop is guaranteed to run at least once, even if the condition
expression already evaluates to true.


Syntax

Written for

Institution
Course

Document information

Uploaded on
March 1, 2025
Number of pages
6
Written in
2024/2025
Type
Class notes
Professor(s)
W
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
smocharingoz

Also available in package deal

Get to know the seller

Seller avatar
smocharingoz
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
17
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions