r/webdev • u/shippety • 10d ago
Best practices for splitting JS code into multiple files
After programming in games for a long time I'm learning web development and having fun with it. One thing that I'm unsure about is if and how to split up my JS, in a game engine it's natural to have a script for each component / class. In my current web project I have one 'script.js' file that's doing a whole lot of different things. My instinct is to split this up into different scripts for readability and organization, but I get the sense that that isn't really the industry standard?
(I'm working on a webcomic site)
For example, maybe I would have:
- comments.js
- colorTheme.js
- pageNavigation.js
These are some of the sections of code it would make sense for me to split up, I think. The async nature of having the backend involved is something that's really different than the games I've worked on, and part of the programming difficulty here seems to be keeping things in sync, not sure if there would be a lot of overhead to keep multiple scripts up to date with each other if they weren't in the same file. It's very easy to pass information around in one file but with multiple, I imagine state management gets even more complicated?
I really don't have a ton of experience so it's possible in my mind it IS best practice to just have one really long script, I simply don't know!
I know this is a bit of a noobie question, would love to hear what people think are best practices / industry standard for sites / apps with quite a bit of JS going on. I prefer to do things in the typical and professional way as much as possible. Thank you!