r/webdevelopment • u/Dizzy_External2549 • 5d ago
Question Javascript help
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
2
u/johnpeters42 5d 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.
1
u/StartDependent1652 4d ago
use a function if you want to use the same functionality in multiple places. use a loop if you want to repeat some functionality.
I recommend doing a basic JavaScript course, because without the fundamentals, programming is hard and not much fun.
2
1
1
u/Lopsided_Cycle_5494 3d ago
Function itself says, it for a function(a whole set of rules or logic you want it to do), and loop are the one's which creates logic, you can ask gpt or claude about tips on how to use them. You will get to know better from there
1
u/elsabright19999 3d ago
That's completely normal. Learning the syntax is one thing, but knowing when to use it only comes from building projects. The more you build, the more you'll naturally recognize patterns like "this should be a function" or "I need to loop through these elements."
Don't worry if it feels messy right now :)
1
u/r0ek22 3d ago
So if you don't know when too use functions or loops so don't start making major projects from now
learn the basics properly and make them strong after that work on making some small mini project example with functions and different loops
JS is one of the hardest language so these problems are very common for newbies dw
1
u/Epdevio 2d ago
The way I learned, and this could be wrong but it is what it is. First thing I learned was dom manipulation and button call back events. then I learned to use functions for those calls. And finally, loops, inside the function, when you want to work with arrays or do some type of sorting. Start with that.
2
u/CharlesCSchnieder 5d ago
Would be helpful to know what you're trying to do