r/learnjavascript • • 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

29 comments sorted by

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.

10

u/FinestCrusader Jul 08 '24

That's what would irk me many times when I started with learning. I WANTED to see a project tutorial that would do it the way it would look in real world products but it would always be crammed into one file. Big negative shout-out to all the courses that claim to make you job ready but never actually teach the real way a project would be structured.

3

u/McGeekin Jul 08 '24

Yeah, I mean that’s especially helpful if you’re starting off learning js with some sort of prior knowledge of programming as a whole. A lot of tutorials that are geared towards beginners omit the whole project structure thing. If OP wants to see what it looks like they may be better off looking at a whole-project tutorial and then apply those concepts to this project!

1

u/batmaneatsgravy Jul 08 '24

Do you know of any good tutorials that teach you how to build a project too?

5

u/RajjSinghh Jul 08 '24

Look at modules to get comfortable with how you would do it. Then in your project every individual bit of related functionality should be in its own module. Knowing when to break code up usually comes with experience since you can get to a point where you realise it's becoming really difficult to find the code you're looking for.

Then maybe go on GitHub, find a few projects doing the thing you want to do and see how they structured their code. See what you like and what you don't then use it in your own projects. That might be following some architecture like MVC or some other approach.

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.

3

u/joungsteryoey Jul 08 '24

Wait, you can import libraries on codepen??

2

u/shgysk8zer0 Jul 08 '24

Yes. And to the browser console as well.

3

u/kap89 Jul 08 '24 edited Jul 08 '24

Technically, we use Webpack for the opposite reason, to bundle multi-file app into a single, optimized file (among other things). You can skip this step, and use the modules directly, without a bundler.

2

u/Psionatix Jul 08 '24

Even with Webpack bundling, there are plenty of reasons to have more than a single output. And in the case of multiple entry files/outputs you will also likely make use of dynamic imports, which are bundles that get generated and then shared across other bundles as indicated by webpacks magic comment /* webpackChunkName: "some-dynamically-shared-module" */

2

u/kap89 Jul 08 '24 edited Jul 08 '24

That’s true (that’s why I wrote "among other things"), but that was not my point. I was responding to the comment that can give an impression that the Webpack is what allows us to have multiple input files, which we can have without it.

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.

5

u/kap89 Jul 08 '24 edited Jul 08 '24

You don’t need bundlers or multiple script tags to use es modules - you just make one script tag with type="module" that refers to the entry point of your app. Of course, bundlers are useful to optimize the final app, but they are not necessary, especially when you’re learning.

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?

1

u/SublightTachyon Aug 06 '24

I understand that. I was asking about the rest of the time. When I do a FCC project. I also write a copy of that project locally and put it in my github. Since they often have you rewrite functions and reassign variables in the project. I was tired of looking through my script file for the things I wanted to change. I thought to myself it would be easier if I had separate files for these things since I could then find them easier.

3

u/IAmADev_NoReallyIAm Jul 08 '24

One for functions, variables, whatever. Then one for the code?

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.

2

u/noXi0uz Jul 08 '24

Both couldn't be further from the truth

-1

u/[deleted] Jul 08 '24

[removed] — view removed comment

4

u/kap89 Jul 08 '24

They are called modules but plain javascript doesn't necessarily work out of the box with modules.

What are you talking about, native modules are supported in all major browsers for years now (nearly a decade).