hello guys welcome to the next blog on JavaScript tutorial for beginners in this blog we will see how to use loops in JavaScript and in this particular blog I'm going to show you how to use while loop and do-while loop in JavaScript so first of all what is a loop a loop is a piece of statement which will allow you to perform some piece of code again and again until and unless some condition is fulfilled right so let's say I have a variable called a and I will initialize it with zero okay and I will initialize one more variable called result and this I will use it to display some value now let's see how the while loop looks like so you use a while keyword and then here comes your condition right and then in the curly braces comes your code if your condition in this bracket is mad okay so this is for the condition this round bracket is for the condition and if your condition is met then the code inside these curly braces will be executed okay and if the condition is not met then it will once again perform the same code until and unless this condition is met so let's say we want to you know have the condition a is less than 10 okay so right now obviously a is equal to zero which is less than 10 so it will go inside the while loop and will perform the code right so for example I will just say result is equal to a okay and for the simplicity I will just add a break tag here so just add this break tag so that it will be printed on the different line every time this code is executed right and this result I'm just passing here which will print this using this demo ID and I have this paragraph with ID demo here so in this paragraph this will be executed right now here if you notice that this condition will never be fulfilled if we write this kind of code right this condition is in finite condition because we are not increasing the value of a so the value of a is constant so our while loop will go on performing the code again and again in finitely write and this kind of infinite loop are also useful in some conditions but right now in our condition what we want to do here is let's say we want to increase the value of a by 1 every time it goes inside the loop ok now what I'm going to do here is I'm going to add this plus symbol or concatenation symbol here and what it's going to do is it's going to add the value of this string to this result every time it executes this code and one more thing what I'm going to do here is I'm going to initialize this result with empty string something like this ok so the value of result will not be ambiguous when it's initialized right so save your code and run it here and now you can see this is printing from 0 to 9 okay so what happens here so in your while loop we know that this condition is obviously true 0 is less than 10 so it will go inside the while loop and perform this code and what this code is doing it's just assigning the value of a in the result right and then it goes here in the next line and it increases the value of a by 1 so the value of a becomes 1 here because it was initially 0 and then we add one here using this code and the value of a is 1 so it goes once again in the loop and it's check 1 is less than 10 yes so it performed this code once again and add this string to the result once again and then the value of a becomes 2 here right so in the in this way it will keep on looping 2 3 4 until 9 reaches and in this case also this condition is true and will increase the value of a by 1 and then value of a becomes 10 which in here is false so 10 is not less than 10 so this condition is not true so this code will not be executed right so that's why it's printing the value from 0 to 9 ok now you can have something like this a is less than or equal to 10 okay so in this case it will print from 0 to 10 because we are also evaluating equal to now okay or you can have different conditions here for example a is greater than or a is not equal to 10 so you can evaluate different kind of conditions in this while loop here okay so this is how your while loop works now there is one more kind of loop which is called do-while loop okay and the syntax of this do-while loop is something like this so you use the keyword do first and then after the curly braces you say while okay and then a semicolon okay so in here it's the same you evaluate some condition inside these brackets right so what I'm going to do here is I'm going to declare one more variable and I'm going to name it as B and I'm going to initialize it with zero and this B I'm going to evaluate in this while loop condition so be is for example less than or equal to 10 the same situation which we have evaluated in here okay and we will just copy the same code from here and paste it in do-while loop and let's define one more valuable for example result 1 which is for do-while loop result 1 ok and let's define one more paragraph tag and this is demo 1 and here also I will define one more document dot get element by ID for demo 1 and here I will print result 1 ok and this will be B and this will be B ok so let's see what happens this is our while loop and this is our do-while loop ok so let's save the code and refresh the webpage and you can see here it prints the value from 0 to 10 using the while loop and once again when I go down it prints the value of 0 to 10 using the do-while loop so they are working in a same manner right so you will say what's the difference between them if they are working in the same manner so the basic difference between while loop and do-while loop is that while loop evaluates the condition first and then execute your code while the do-while loop performs your code for the first time and then evaluates your condition right so do-while loop no matter what at least one will execute the code inside these curly braces even if this condition is false so it's logical right it says do first and then evaluate the condition and in here we are first evaluating a condition and then executing the code so let's say we are evaluating that is for example - one right and in here also as B we are evaluating the condition minus one which is false because B is equal to zero right and in the while loop also a is equal to zero right so save your code and refresh the webpage and it prints zero which is due to this do loop so let's say we say do and here we say while so we can recognize from which line this zero is printed so just save your code and refresh it once again and you see once after do is printed then zero is printed right so it's coming from this line okay so what's happening here so here this condition is false so this code will not be executed but in here in the do-while loop first the code is executed and then the condition is evaluated so this code is executed once and then it goes to while loop and checks whether this condition is true or false and it's false so it's not going to loop it once again so this is the basic difference between while loop and do-while loop so I hope you enjoyed this blog