I am starting a new mini series on JavaScript. I’m starting it from zero. Anyone can start learning from here. My goal is helping others while learning! I know about JavaScript and I’m working on a MERN tech stack. But I think it is not a bad idea to revise the JavaScript from scratch and helping others in this time. If you have any questions, shoot on the comment session or you can directly ask me in my social media. Social media links are in the last part of today’s lesson! Let’s learn JavaScript together.

While Loops — JavaScript Series — Part 13

A full series to learn the JavaScript from zero

Muhammad Ali
3 min readFeb 2, 2020

Suppose we are going to print 1 to 10 on the console. But how we can do it ? We can do it easily

console.log(1)
console.log(2)
...
console.log(9)
console.log(10)

But this is not a good way to do it. Currently we can do it because it’s only ten times. But suppose we need to print 1–100. How to do it ?

This is where we use loops. We’re going to use while loop today!

While is easy. Just remember what we learned in our last tutorial. While loop syntax is –

while (condition) {
// code block to be executed
}

Let’s write some real codes now. If we wants to print 1–100 then we are going to store the values in variable and we are going to start from 1. So var number = 1

Now we are going to start our while. While and the condition will be.

while (number < 101) {
//code block that we wants to do
}

Now we are going to print the values from the first one. We need to increase the value of number to break the condition, if the condition doesn’t break then it will stuck forever. So we always break the loop condition

See the result in browser

Do you understand the while loop ?

Originally it published on nerdjfpb blog. Check the git for codes.

you can connect with me in twitter, linkedin or instagram

--

--

Muhammad Ali
Muhammad Ali

Written by Muhammad Ali

An otaku who loves computer. Love to write and help others through my work. Have a look on my Instagram https://www.instagram.com/nerd_jfpb/

No responses yet