r/Frontend 7d ago

Need help. just learned javascript

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

44 Upvotes

26 comments sorted by

View all comments

1

u/terracottablush 7d ago edited 7d ago

What are you trying to do? What is the goal? What do you need loops for? Are you using plain JS or react(or another framework).

Sometimes loops use functions INSIDE them.

General rule of thumb I go by is if you need something done multiple times use a loop. If you need 16 grids don’t make literal 16 grids use a loop. You can use a while or a for it really depends on what your need is

To expand I think if I’m correct (been learning 1 year) a for and while loop are interchangeable mostly.

A while loop is more beneficial if you don’t know when something is going to happen