FOR LOOPS IN JAVASCRIPT (Javascript tutorial #11)

in blurtutorials •  13 days ago  (edited)

Today we will learn about For Loops in Javascript.

images_20201017224616806.jpg

So what are For Loops and why should someone use it :
The definition will tell us both the things about what are for loops and why do we need them.

Suppose you want to print number one to 100 so you won'tlove to write:-
1.) console.log("1")
2.) console.log("2")
3.) console.log("3")
.
.
.
.
.
100.) console.log("100")

So what if there is a way to write a code of few lines to make it all work...?
Interesting right...? Well let's see how can you do that.

Step By Step Demonstartion on how to use For Loops:

Step 1 : create a html structure just like all the other tutorials.

Step 2 : Create following script tag and write the code as it is.

Screenshot from 2020-10-17 22-18-32.png

Step 3 : Run your file with the browser and press F12 to see the result.

Screenshot from 2020-10-17 22-40-39.png

Full Code :

Screenshot from 2020-10-17 22-18-34.png

Further Explanation :

In the Above code when we have used for loops what we are doing is :-

  1. first we initialiaze i = 0 .
  2. second we compare if it's less than 3 ( 0 is less than 3 so the condition here is true so the code in brackets will execute).
  3. lastly we increment i.e we increase the vale of i by 1 and repeat what we do in step 2 i.e we compare if i is less than 3 again and we continue to execute the code until the condition become false.
  4. So when i is 0 we have console.log ---> console.log("Person at rank " + (i+1) + " is " + blurtpower[i]);
    It will print that Person at rank 1 is @double-u as value of blurtpower[i] for the first case is @double-u.
    Similarly for i = 1 the console will print Person at rank 2 is @sct.blurt.
    i = 2 the console will print Person at rank 3 is @tomoyan.

When i becomes 3 then condition becomes false as 3 is not less than 3.
That's all about the tutorial.
Ask Your Questions below.

Thank You.


Links to all the tutorials are shared below

  1. Javascript , Alert Message
  2. Lets Play with buttons Using Javascript
  3. How To Change Html content using Javascript
  4. Data Types And Variable in Javascript #1
  5. SWAPPING TECHNIQUE IN JAVASCRIPT
  6. OBJECTS IN JAVASCRIPT #1
  7. OBJECTS IN JAVASCRIPT #2
  8. OBJECTS IN JAVASCRIPT #3
  9. ARRAYS IN JAVASCRIPT #1
  10. If and Else Statements in Javascript
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  

hi @kamranrkploy

Thank you for providing java script tutorials. i am learning python+selenium now and looking forward to know more about javascript as well and your information is definately gonna be a helpful for me. Great post !!!!

Everyone come Join the Blurt Group on Steemit so we can help Blurters earn STEEM Backed Dollars to buy BLURT
https://steemit.com/trending/hive-124988