JavaScript While and Do While Loops

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript While and Do While Loops

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

while

const people = ['jane', 'john', 'jack', 'joe'];

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

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

console.log(i); // 4

// remove a person one by one and log it
let person;
while (person = people.pop()) {
console.log('Popped:', person);
}

console.log(people); // []

do while

/**
* Log out fibonacci numbers
* which are less than 100
*/

let current1 = 0;
let current2 = 1;

do {
console.log(current1);
[current1, current2]
= [current2, current1 + current2];
} while (current1 < 100);
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

For most looping needs In JavaScript, you will most likely use the for loop. But when you are getting values only on demand, then the while Loop is an excellent candidate. Consider the simple array consisting of people, Jane, John, Jack, and Joe. If you want to log out the names of all of these people, we can do that quite easily. With an index based for Loop, we can actually achieve the same effect with the while loop provided by JavaScript. The while loop is syntactically much simpler than a fall loop because it only consists of a condition and a while loop body. It starts off by first checking the condition. If the condition is true,

00:34

it's going to execute the body and then it's going to check the condition again. And as long as the condition is going to be true, the body is going to execute for another iteration. Unlike the fall loop, we are responsible for doing our own initialization, which we are doing with the simple statement before the Y loop. And we are responsible for our own afterthought, which we are doing as an additional statement in the body of the Y loop. There is a slight advantage of using a fall loop here in that it doesn't leak its initialization variable to the after scope. However, the Y loop does, which can result in variable conflicts with other loops. However, with the Y loop,

01:07

you do have more control in how you want to structure your initialization and the afterthought. A strong reason for using the Y loop is when your action is going to have an impact on the items that you are iterating over. As an example, consider wanting to remove the person one by one from the people array and then logging it out. This is quite easy to do with a Y loop because we simply pop the last item, store it in a person variable and then log it out. This sort of thing is not a great experience for a fall loop, as you really shouldn't use a fall loop and the item you are iterating over might change. Once this while loop terminates,

01:40

the people array should be empty and we can verify that with a simple log statement. And when we run this code, you can see that it matches our expectations. Sometimes you want to execute the body of the while loop at least once, irrespective of the condition being true. And for that, we can use the do while loop. Consider the example where we want to log out the Fibonacci numbers, which are less than 100. We know that the first Fibonacci number is zero and the second Fibonacci number is one. Since the initial Fibonacci number is less than 100, there is no point in checking if it is less than 100 upfront. So we use a DOA loop within the body. We log out the current Fibonacci number,

02:17

and then we move the next Fibonacci number, which is current two into current one, and calculate the next upcoming by simply adding current one plus current two. Now, we will repeat this process as long as the current Fibonacci number is less than 100, and for that we add the vial with the condition. That current one should be less than 100. And if you run this code, you can see that it works as expected. We start off with zero than one and then continue till current one becomes greater than 100. I hope you enjoyed this short focus tutorial. As always, thank you for joining me and I will see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36