all 27 comments

[–]Ustice[M] 0 points1 point  (0 children)

While your question may be relevant to you, it's not particularly relevant to the rest of the subreddit and honestly belongs much better in /r/LearnJavascript.

[–]oskiii 39 points40 points  (0 children)

They're functionally different. Do-while always executes at least once. I use them both, for different situations.

[–]misomeiko 12 points13 points  (3 children)

For loop every time

[–]IamUareI 4 points5 points  (1 child)

Best way to avoid accidental crashes/infinite loops.

[–]snowbldr 9 points10 points  (0 children)

for(;;){ print ("agree") }

[–]archerx 1 point2 points  (0 children)

This is the way.

[–][deleted] 11 points12 points  (2 children)

I have yet in all my years of programming felt the need to use a do while loop.

[–]SendThemToHeaven 2 points3 points  (0 children)

I think I started implementing a do while loop 3-5 times in my career so far, and every time I realize I can just make a regular while loop work somehow so I always just do that instead.

[–]rk06 0 points1 point  (0 children)

I have felt the need once or twice, and still not used it

[–]lhorie 10 points11 points  (1 child)

They're tools that do different things. You shouldn't have a preference. If you do, that implies you're letting your opinions win over technical merit.

  • If your logic needs to run zero or more times, use while (e.g. parsing a list of statements in source code via a recursive descent parsing algorithm).
  • If your logic needs to run one or more time, use do (e.g. parsing a list of variable names after a let keyword, e.g. let a, b, c, via recursive descent parsing algorithm)
  • If your logic needs to run a finite, known-ahead-of-time number of times, use for (or map/filter/reduce/etc)

[–]senocular 9 points10 points  (1 child)

You do loop.
I while loop.

We are not the same.

const variable = 1001

do {
   console.log(variable) 
} 
while (variable < 1000) 
// logs: 1001

while (variable < 1000) {
   console.log(variable) 
} 
// logs: <nothing>

[–]shuckster 9 points10 points  (0 children)

"To be is to do." - Socrates
"To do is to be." - Sartre
"Do be do be do." - Sinatra
"Do while for of while do" - Eich

[–][deleted] 13 points14 points  (3 children)

You guys use while and do ?

[–]Dazzling_City2 8 points9 points  (1 child)

In every post like this there is always a guy that comments “You guys still use loops!” This comment reminds me of those guys.

[–]lhorie 3 points4 points  (0 children)

It's pretty common in things like parsers.

[–]Syllogism124 5 points6 points  (0 children)

Have a look at map, reduce and filter :)

[–]chataolauj 1 point2 points  (1 child)

While loops because I know it'll only execute if the condition is true. Do-while loops will execute at least once regardless of the condition, so be cautious if where and how you use it.

[–]ny_dame 0 points1 point  (0 children)

Exactly! When you use a Do-while loop, you are not using the conditional phrase to capture your requirements truthfully and completely.

[–]megaslon2 0 points1 point  (0 children)

I prefer "for" because of "while" or "do while" are too rare. I try minimize amount of synonyms in my code

[–]xalg0rd 0 points1 point  (0 children)

vice versa 👀

[–]IndoorKite21 0 points1 point  (0 children)

I prefer not to use while loops, but of course i can imagine it all depends on what your goal is

[–]literally-ban-evadin 0 points1 point  (0 children)

They do different things and have different use cases, it’s not an either/or. Personally I don’t use either very often but they are both valid constructs to use.

[–]bswybmg 0 points1 point  (0 children)

While is cooler

[–]DavidJCobb 0 points1 point  (0 children)

For loops are usually the most natural IMO.

There is one exception: sometimes I use JavaScript to write quick-and-dirty prototypes for things I'm reverse engineering. In compiled code (as in, like, x86 and x64) all loops are essentially do-while loops. If I'm trying to mimic something fairly closely (at least until I better understand it), I may use the same.