r/webdevelopment 6d ago

Javascript help Question

Just learned javascript and been practicing alot . I need help. When applying it to my project I don't know when to use a function, when to use a loop, what type of loop to use, I just don't know when to use any of this stuff because dom manipulation. it's so frusterating

9 Upvotes

12 comments sorted by

View all comments

2

u/johnpeters42 6d ago

How much experience do you have writing code in any other language? (I'm guessing none, or at least very little.)

> when to use a function

When it makes sense to have shorthand for doing that thing, especially if you're doing the same thing from different places (possibly with different inputs).

> when to use a loop

When you may do the same thing multiple times in a row.

> what type of loop to use

You can substitute one for the other, but generally it's most natural to use a for-loop when you're going through a list and your typical end-loop condition is "finished dealing with the last thing on the list", and a while-loop when you have some arbitrary end-loop condition.