#4 Javascript Loops| Javascript Hindi Course For Beginners ( 2023 )
Why we use Loops ?
We use loops to do repeated actions.
// without loops // print 1 - 10 number on console console.log(1);console.log(2)console.log(3); console.log(4);console.log(5);console.log(6); console.log(7);console.log(8);console.log(9); console.log(10); // using loops for(let i = 1 ; i <= 10 ; i++){ console.log(i ) } // Look above line of code i have used loops reduce code // no need need to write 10 lines of console log 🤣
Types of loops in Javascript
- while
- do-while
- for
- forEach()
- map()
- for…in
- for…of
While Loop
while loop is most basic loop in javascript.
While loop runs a code block , as long as condition is true.
// Syntax while (condition) { // Block of code }
var i=8; while (i<10){ console.log("I is less than 10"); i++; } // Output /* I is less than 10 I is less than 10 ... 10 times */
Do While Loop
Do-while loop is slightly different from while loop.
Its has only one feature extra.
Do While loop code is executed at least once if condition satisfies .
If condition satisfies the code block will be executed like while loop.
// Syntax do { // Block of code } while (condition);
var i=7; do{ console.log("The value of i is " + i); i++; } while(i>7 && i<10); // Output /* The value of i is 7 The value of i is 8 The value of i is 9 */
For Loops
For loops and while loop work exactly same.
But For loops have much advance than while loop.
you can add
initialization
in for loop directlyyou can add
condition
in for loopyou can also add
final-expression
in for loopIn case of while loop . you can only add
condition
// Syntax for ([initialization];[condition];[final-expression]){ // Block of code }
for (let i=0; i<10; i++){ console.log("The Value of i is : " + i); } // Output /* The Value of i is 0 The Value of i is 1 ... The Value of i is 9 */
Currently we are skipping this topics , after learning object and array we will learn it
forEach()
map()
for…in
for…of
Questions:
- Print Even number upto 60 using for loop
- Given a number n Calculate the factorial of the number
- Write table for 19 using loop
- Write a program that will allow someone to guess a four digit pin exactly 4
times. If the user guesses the number correctly. It prints “That was
correct!” Otherwise it will print “Sorry that was wrong.” Program stops after the 4th attempt of if they got it right.
let pin = 0704; // Example output: // Please make your guess: // 4544 // Sorry that was wrong. // Please make your guess: // 4444 // Sorry that was wrong. // Please make your guess: // 0704 // That was correct!
Javascript Loops | Javascript Hindi Course For Beginners ( 2023 ) Complete Notes of JS - https://dosomecoding.com/courses/javascript/javascript-hindi-course-for-beginners Javascript Beginners Playlist Link - https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion HTML Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gL2isb9Kr1GvTM8id2gdtY CSS Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gWK5TVILnKSvuc9Fc15sbH Html and CSS practice Projects https://www.youtube.com/playlist?list=PLPppPPmk0i3hZCNmbVtcP1hlwDKOdUFX9 Javascript Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion