For VS While Loop in Javascript

When we need to perform similar actions for certain number of times, that is when we get to “Looping”.

For example, when we need to output goods from a list one after another. Or just run the same code for each number from 1 to 10.

Loops are a way to repeat the same part of code multiple times.

In Javascript, there are while loop, for loop and do-while loop. In this post, the while and for loop will be compared.

The syntax for while loop is :

while (condition) {
  // code
  // so-called "loop body"

the syntax for for loop is :

for (begin; condition; step) {

  // ... loop body ...


So what is the deal with for loop?

For Loop allows you to run through the loop when you are certain as to how many times you would like it to run through the data structure such as for (var i; i < 9; i++); this will continuously increment i until the condition returns false (i is no longer less than 9). Any number can replace the 9 even a different type of variable. But the loop will terminate once the condition is no longer met.

How about a while loop?

A while loop can be used when you need your loop to run an unknown number of times until a specific condition is met.

Let’s use a contrived example. If you are making a tax calculator that allows a human resource manager to see how much tax that each employee out of ALL employees has to pay. The person who uses the calculator will have to input the number of employee that the loop will iterate through, like so :

let totalEmployee = prompt("Please input the total employee");

let i = 0; // the iterator

let tax = {}; // key-value pair for calculated tax for each employee

while(i <= totalEmployee){ ... assign the value of the tax for each employee here ... } 

// execution stops after counting the tax of the last employee

So how are they different from each other?

The key differences between the two loops lie in the intention “why do we need looping for?” and “do we know the condition at which the loop will stop?”

Use for loop if you know the condition or when to end or break the iteration.

Use while loop if you were to use an existing variable (purposefully using VAR keyword that is not block-scoped) in your program; or when your condition uses a new value that is obtained through the interface (like prompt / asking a user to input a number in the calculator example above).

Special to JS though, the iterating variable i for instance can be bound within the block of the for loop if it is defined by the let keyword which makes the for loop safer for some inexperienced JS programmers.

Long story short : The for loop is mostly preferred for the simple iteration jobs while the while loop is more applicable when you need to implement something at a frequency of iteration that is not yet certain or is a dynamic value that will depend on the interaction with other aspects/variables in your program.

Why let for the for-loop?

It has something to do with block-scoping for variables.

Say you have a program with one for-loop in it.

If your for loop has i instance, when you also have i in the outer scope like so :

// outermost scope of the program

var i = "Hello World";

console.log("what is i : i is", i); // prints "What is i : i is Hello World"

// but then you have for-loop below

for (var i = 0; i < 9; i++) { 
  // block-scope of for loop here 


// let's use the same variable i below :

console.log("what is i : i is", i) // what will be printed to the console? what will be the value of i?

In the example, we are touching the issue of “block scoping”. Read more about it here.

After the closing curly brace, the execution goes back to the outer scope. If you need the same i instance, that was assigned to be “Hello World” before the for-loop begins, the i instance will no longer have the same value. The i in the for loop will override / reassign the value of i, resulting in the console.log to display :

What is i : i is 9 // not "Hello World"

This problem is caused by the fact that var is not block-scoped, so the value spills over all over places, in and outside for-loop. This can be mitigated with the use of LET in replacement of VAR. It is all due to LET having the power to turn variables only applicable within its block.


  1. StephenLag October 27, 2020 at 10:51 pm

    viagra 100mg price buy viagra cheap viagra

  2. Jamesweemn October 31, 2020 at 4:33 am

    generic cialis available generic cialis 30 day cialis trial offer

  3. Traviskeevy November 1, 2020 at 12:12 pm

    where to buy viagra online viagra for sale viagra without a doctor prescription

  4. EddieSiz November 2, 2020 at 3:32 pm

    help with ed ed cures that work best male enhancement

  5. Jameskeymn November 4, 2020 at 4:12 am

    generic viagra online buy viagra online price of viagra

  6. Jameskeymn November 4, 2020 at 5:43 pm

    generic viagra without a doctor prescription generic viagra viagra prescription online

  7. Jameskeymn November 5, 2020 at 7:16 am

    best place to buy generic viagra online cheap ed pills can you buy viagra over the counter

  8. Jameskeymn November 5, 2020 at 8:50 pm

    generic viagra online for sale cheap ed pills viagra online canada

  9. ErnestRaili November 7, 2020 at 4:42 pm

    best ed pill Cheap Erection Pills buy prescription drugs without doctor

  10. ErnestRaili November 8, 2020 at 5:10 am

    best pills for ed generic cialis ed clinics

  11. ErnestRaili November 8, 2020 at 6:07 pm

    buy online pharmacy ED Pills amoxicillin without a doctor’s prescription

  12. Dannyket November 10, 2020 at 7:11 am

    cialis for peyronie buy tadalafil cialis professional

  13. Dannyket November 10, 2020 at 8:27 pm

    cialis vs levitra buy cialis cialis coupons 2019

  14. Brucebef November 13, 2020 at 10:52 am

    best ed pills at gnc generic ed pills errectile dysfunction
    solutions for ed

  15. Brucebef November 14, 2020 at 12:08 am

    ed treatment review ed medications online ed doctor
    ed medications online

  16. Brucebef November 15, 2020 at 4:17 am

    homepage canadian medications doctors for erectile dysfunction
    over the counter ed treatment

  17. Williamsnums November 18, 2020 at 9:27 am

    drugs from india buy generic drugs online

  18. Williamsnums November 20, 2020 at 4:20 am

    india pharmacy drugs from india

  19. AngeloSheer December 3, 2020 at 2:20 pm

    pumps for ed cheap Prednisone prescription drugs online without doctor

  20. AngeloSheer December 4, 2020 at 6:32 am

    cialis lowest price 20mg cialis for sale how does cialis work

  21. AngeloSheer December 4, 2020 at 3:43 pm

    viagra cialis generic viagra where to get viagra

  22. AngeloSheer December 5, 2020 at 1:01 am

    cheap medications online buy Nolvadex buy cheap prescription drugs online

  23. AngeloSheer December 5, 2020 at 10:08 am

    cialis vs levitra generic cialis cialis free trial

  24. AngeloSheer December 5, 2020 at 7:17 pm

    warnings for cialis generic cialis where to get cialis sample

  25. Gregoryror December 7, 2020 at 7:35 am

    what causes ed: canadian medications ed treatments that really work

  26. Gregoryror December 7, 2020 at 4:12 pm

    errection problem cure: natural drugs for ed sexual dysfunction in men

  27. Gregoryror December 8, 2020 at 10:52 am

    drug store online: best ed pill cheap ed pills

  28. Gregoryror December 8, 2020 at 8:16 pm

    do i have ed: how to cure ed what causes ed

  29. MasonRourb December 11, 2020 at 10:04 pm

    daily use cialis cost otc cialis interactions for cialis

  30. Suttgaith December 13, 2020 at 1:58 am

    cialis work for women accuttincant cialis online Mentitunty Generic Real Isotretinoin Best Website C.O.D. Cheapeast Shop

  31. MichaelBef December 15, 2020 at 4:14 pm

    Flagyl antibiotics Amoxil antibiotics prescription drugs online without doctor

  32. MichaelBef December 16, 2020 at 8:47 am

    best ed medications Zovirax buy Zovirax

  33. MichaelBef December 16, 2020 at 5:13 pm

    amox k 875 125 buy Flagyl cheap pills online

  34. CharlesjaW December 18, 2020 at 12:17 am

    tadalafil without a doctor’s prescription ed pills that work 100mg viagra without a doctor prescription

  35. CharlesjaW December 19, 2020 at 6:07 am

    natural ed drugs canada drugs canadian online pharmacies

  36. CharlesjaW December 19, 2020 at 11:32 pm

    buy prescription drugs from canada canada online pharmacy fda approved canadian online pharmacies

  37. CharlesjaW December 20, 2020 at 7:53 am

    cialis and interaction with ibutinib cialis 20 mg best price cialis prices

  38. Williamsoake December 22, 2020 at 9:03 am

    hjnmcdlt viagra viagra cost

  39. RobertLaf December 23, 2020 at 9:16 pm

    where can you buy zithromax drug bactrim antibiotic smz tmp ds

  40. RobertLaf December 24, 2020 at 5:54 am

    zyrtec 10.00 coupon periactin canada otc allegra over the counter coupon

  41. RobertLaf December 24, 2020 at 11:51 pm

    yasmin pill cost uk alesse for acne alesse for acne

  42. RobertLaf December 25, 2020 at 9:55 am

    bactrim medicine bactrim ds medication doxycycline hydrochloride 100mg

  43. RobertLaf December 25, 2020 at 6:39 pm

    order clomid yasmin uk buy serophene tablet

  44. RobertLaf December 26, 2020 at 3:28 am

    zyrtec australia zyrtec 90 benadryl 5 mg tablet

  45. RobertLaf December 26, 2020 at 12:13 pm

    acyclovir online pharmacy zovirax cream price acyclovir buy online

  46. RobertLaf December 26, 2020 at 9:15 pm

    how much is generic valtrex zovirax online uk famvir 500 mg price

  47. James Campbell December 28, 2020 at 5:57 pm

    Woah! I’m really enjoying the template/theme of this
    blog. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and visual
    appeal. I must say you’ve done a excellent job with this.
    Also, the blog loads very quick for me on Internet explorer.
    Excellent Blog!

  48. Pingback: cheap cialis generic

  49. Bonnie Williams December 29, 2020 at 12:31 pm

    I do accept as true with all of the ideas you have presented on your post. They’re really convincing and will certainly work. Still, the posts are too short for newbies. May just you please prolong them a little from next time? Thank you for the post.

  50. Pingback: how hard can i get with cialis vs viagra vs levitra

  51. Luz Zhang December 30, 2020 at 3:16 pm

    It’s truly very complex in this busy life to listen news on TV, thus I just use internet for that
    purpose, and obtain the most up-to-date information.

  52. Sharon Rankin December 31, 2020 at 1:16 am

    I blog frequently and I genuinely appreciate your information. The article has truly peaked my interest.
    I will take a note of your website and keep checking for new information about
    once a week. I subscribed to your Feed as well.

  53. Pingback: marley drug viagra

  54. George Morton December 31, 2020 at 11:29 am

    Hey would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot faster then most.
    Can you recommend a good internet hosting provider at a fair price?
    Kudos, I appreciate it!

  55. Pingback: viagra trial sample

  56. Charles Klopp January 2, 2021 at 12:01 pm

    I seriously love your site.. Excellent colors
    & theme. Did you build this amazing site yourself?
    Please reply back as I’m looking to create my own personal site
    and would love to find out where you got this from or exactly what the theme is named.

    Thank you!

  57. John Combs January 2, 2021 at 8:32 pm

    Hey there just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Chrome.

    I’m not sure if this is a format issue or something to do with
    internet browser compatibility but I thought I’d post to let you know.
    The design and style look great though! Hope you get the issue fixed soon. Cheers

  58. Pingback: cialis pro 40

  59. Amanda Kristof January 3, 2021 at 9:05 pm

    I love what you guys are usually up too. Such clever work and coverage!
    Keep up the excellent works guys I’ve incorporated you guys to my own blogroll.

  60. Christopher Hazel January 4, 2021 at 9:48 pm

    Hi would you mind sharing which blog platform you’re working with?
    I’m planning to start my own blog in the near future but I’m having
    a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something
    unique. P.S My apologies for being off-topic but I had to ask!

  61. Brett Principio January 5, 2021 at 11:41 pm

    I’ve been surfing online more than 2 hours today, yet I never found any interesting article like
    yours. It is pretty worth enough for me. Personally, if all site
    owners and bloggers made good content as you did, the net will be much more useful than ever before.

  62. JasonSom January 6, 2021 at 11:03 pm

    valtrex over counter buy valtrex without get a prescription online

  63. Nicole Mcgill January 7, 2021 at 1:12 am

    I will immediately take hold of your rss feed as I can not in finding your e-mail subscription hyperlink
    or e-newsletter service. Do you’ve any? Kindly let me realize in order that
    I may subscribe. Thanks.

  64. JasonSom January 7, 2021 at 5:55 pm

  65. Abraham Waugh January 9, 2021 at 6:11 am

    Hi there! I could have sworn I’ve visited this web site before but after looking at many of the posts I realized it’s new to me.
    Anyhow, I’m certainly happy I came across it and I’ll be book-marking it and checking
    back frequently!

  66. Frank Garcia January 9, 2021 at 10:31 pm

    These are genuinely great ideas in about blogging. You have touched some good factors here.
    Any way keep up wrinting.

  67. Pingback: otc viagra us

  68. Nina Zachary January 10, 2021 at 11:35 am

    Thank you for the good writeup. It in fact was a amusement
    account it. Look advanced to more added agreeable
    from you! However, how can we communicate?

  69. Pingback: drug interaction finesteride and cialis

  70. Grace Millage January 11, 2021 at 3:01 pm

    It is appropriate time to make some plans for the future and it’s
    time to be happy. I have read this publish and if
    I may I desire to counsel you some attention-grabbing things or tips.
    Perhaps you could write next articles referring to
    this article. I desire to read even more issues approximately it!

  71. MichaelTut January 11, 2021 at 3:28 pm

    what type of medicine is prescribed for allergies mexican pharmacy without prescription

  72. Pingback: generic viagra without

  73. MichaelTut January 12, 2021 at 12:28 am

    ed meds online without doctor prescription comparison of ed drugs

  74. Raymond Blenker January 12, 2021 at 1:00 am

    I’ve been surfing on-line greater than 3 hours as of late, but I by no means discovered any interesting article like yours.

    It is lovely price enough for me. In my opinion, if all webmasters and bloggers
    made excellent content material as you probably did, the net shall be a
    lot more useful than ever before.

  75. Davidhinue January 12, 2021 at 2:38 am

    buy ed drugs online prescription drugs without prior prescription

  76. Robert Parsons January 12, 2021 at 3:15 pm

    Hi everybody, here every one is sharing these kinds of know-how, therefore it’s fastidious to read this webpage, and I used
    to go to see this weblog everyday.

  77. JasonvaM January 15, 2021 at 7:07 am

    best place to buy viagra online uxoaxlwg sildenafil how to get viagra

  78. JasonvaM January 15, 2021 at 2:08 pm

    generic cialis no doctor’s prescription cialis qlxjplno cialis money order

  79. Davidlycle January 15, 2021 at 8:35 pm

    how much viagra should i take the first time? lliemvgf viagra professional

  80. JasonvaM January 15, 2021 at 9:42 pm

    drug medication ncxrfddd cheap levitra how to overcome ed

  81. JasonvaM January 16, 2021 at 5:04 am

    muse ed drug nnicvqgw order levitra help with ed

  82. JasonvaM January 16, 2021 at 12:16 pm

    cialis maximum dosage buy cheap cialis qldhzubp cialis generic availability

  83. JasonvaM January 16, 2021 at 7:34 pm

    cialis for peyronie cheap cialis cmzuujgv cialis erections

  84. JasonvaM January 17, 2021 at 2:52 am

    natural cures for ed ebiymjlv levitra generic compare ed drugs

  85. uphozy January 17, 2021 at 3:30 am

    where to buy sildenafil in south africa
    30 mg tadalafil
    can you buy viagra over the counter in colombia Chugh Anync

  86. uphozy January 17, 2021 at 1:55 pm

    how long does cialis last 20 mg
    cialis price usa
    mexican pharmacy real pfizer viagra Chugh Anync

  87. Pingback: buy cialis 36 hour

  88. JamesKix January 19, 2021 at 3:25 pm

    cialis discount card cialis online rqlpugrd walgreens price for cialis 20mg
    price of cialis

  89. JamesKix January 19, 2021 at 10:14 pm

    high blood pressure and cialis cialis coupons 2019 ytmhhyih prices of cialis
    coupon for cialis by manufacturer

  90. Pingback: cialis without prescriptions canada

Leave a comment

Your email address will not be published. Required fields are marked *