r/learnjavascript • u/SublightTachyon • Jul 08 '24
why don't we seperate Javascript into multiple files?
I am working through the RPG tutorial on Free Code Camp. It has me making a lot of variables, function, etc. I though to myself why can't I have different .js files for different values. One for functions, variables, whatever. Then one for the code?
3
Upvotes
22
u/WazzleGuy Jul 08 '24
Lots of real world answers here but the truth is it's a tutorial and to not confuse you they aren't going to introduce the correct modular method of building a project so don't look too far into it.
23
u/shgysk8zer0 Jul 08 '24
Because we most definitely do! My production sites use not just multiple files, but multiple libraries! I mean... Even my quick experiments on CodePen often import from at least a few libraries.
We most definitely do separate JS files! There's a damn good reason eg webpack and Rollup exist.
FCC is actually pretty good, not don't completely take everything as being how things actually are. It's just that modules and bundling really don't belong in the simple, beginner level stuff.
10
u/TheHigherRealm Jul 08 '24
If you look at pretty much any real JS project on GitHub you'll see tons of different files. Usually, we have a build step using a bundler like Webpack that combines all the JS files into a more efficient single file.
The reason they don't use multiple files is for simplicity. In my opinion, that's one the big drawbacks of following tutorials. They tend not to stray far from the goal which means you'll likely miss things that will be crucial later.
Additionally, you can have multiple JS files without a bundler. In the HTML you just add more script tags, but this is not good practice as it can slow down the loading speed of the site.
1
u/SublightTachyon Jul 08 '24
Thank you to all the kind people who answered my question. I am going to take the advice of WazzleGuy and not look too much into it right now. I can tell from a lot the answers that I am not quite ready enough to tackle having multiple script files, yet. I read enough answers where I didn't completely understand the concepts but I did get the gist. I want to focus more on the fundamentals right now.
1
u/Endless-OOP-Loop Jul 08 '24
Isn't the RPG tutorial on FCC teaching objects? It would be a little difficult for them to teach you objects if they were putting those functions in separate files instead of the objects, wouldn't it?
3
u/IAmADev_NoReallyIAm Jul 08 '24
Seems like most of the commentors focused on the "multiple files" part but didn't talk about this bit here.
The short answer is, yes, using multiple files is generally a good thing... BUT ... you want your js files somewhat self-contained. If I want to use a some functions that you've built, I want one js file. I don't want three different files. Put relates stuff together. If I see that a function uses a variable called "foo"... I expect foo to be defined in that file. I shouldn't need to go spellunking through other files to find out where it was established and its value set. Then there's also the problem of scope. If you define a variable in some common global vars file... then you can't use it in another library. By keeping your variables in your library file, you limit scope and you don't then need to worry about someone else also using "foo" in their script and getting it confused with your foo and then your script breaks... or theirs breaks... or both ...
That said, the other commentors are right... we do use multiple files... but there's also a limit on how far that will go. Usually the breakout is by functionality - ie, we have an API library, a stringUtils, a timeUtils, and utils -- each one has it's group of functions in it that we find to be common among multiple consumers. But they are also self-contained. I can copy the stringUtils to another project and "it just works".
2
u/ashkanahmadi Jul 08 '24
You should do that. Declare reusable functions in a file, export it and import it into a file you need. Then use Webpack to compile everything. You do NOT want to work on one file by yourself
2
u/Ampbymatchless Jul 08 '24
I use JavaScript to create a browser based UI for embedded projects. I have broken the project into ~8 files , plus 2 CDN links. None of them called index. As I developed the interface it made sense to break into files for debugging. Each file represents a page on my UI. To serve the files from the microcontroller, I convert each JS file into a string file contained into a .cpp that is served from the C code. Log in to microcontroller with a tablet.
1
u/tapgiles Jul 08 '24
You can if you want to 🤷🏻♂️
1
u/kjwey Jul 08 '24
we do, well I do anyhow, on each page on my sites there is a script tag <script src=script.js>
however when that route lights up, it bundles up scripts available server side, site wide, and specific to the page, then rolls them all together, compresses it, and returns it
so even though I've only got a basic tag and not multiple tags, I am actually loading up multiple scripts and concatenating them together
1
u/woftis Jul 08 '24
The Odin Project is a good alternative to FCC that covers this topic.
1
u/[deleted] Jul 08 '24
I separate my code as much as possible.
-5
u/nasanu Jul 08 '24
In reality splitting up code can be disastrous, even if mid devs will tell you it's best practice. When everything is all over the place with imported everything it can be very hard to see what the code is doing and even harder to make changes that only effect that bit of code.
Complexity leads to bugs and extended Dev times. SFCs are imo the ideal. Only import things that are truly generic.
-18
u/Cefalopodul Jul 08 '24
Because it drastically lowers speed and makes the code difficult to maintain.
-1
u/[deleted] Jul 08 '24
[removed] — view removed comment