2

I tried to search, but cannot find precise / concise answer to this question. It appears to be used loosely and interchangeably, IMO. Anyone can teach me?

2
  • 2
    There’s a difference between a script and a module. Whether these are separate files or inline is an orthogonal concern. Currently, a JavaScript file can only be one module, and a module can only span one file. But there’s a proposal to introduce module blocks. Commented Oct 23, 2021 at 17:26
  • 1
    "It appears to be used loosely and interchangeably" - yes. Especially due to "JS file" not being a technical term. And "module" being a generic term, not necessarily referring to concrete ES module definition. Commented Oct 23, 2021 at 17:34

2 Answers 2

1

Well we can get really technical here.

A file is anything on an OS that can handle IO information storage, of which there are various types i.e. text files and directories. A directory is a specific type of file that allows other files to be stored within.

When you speak about a "js file" it's sort of slang for "a text file that has a .js extension and javascript within".

In modern javascript modules are used to create more cleanly separated and reusable modular pieces of code, which can be exported and imported into other modules (technically files). They're sort of like namespaces in other languages (not really but sort of) but each file can only have a single namespace (module).

You can also have js files that are not modules, all modules are files but not all files are modules.

Sign up to request clarification or add additional context in comments.

Comments

1

Good question!

Background: JavaScript files used to be very small but as the JavaScript language has become more popular, the JavaScript files have become so large that "modules" were introduced to to help break up the JavaScript code into smaller, more manageable pieces.

So what does this mean?

Simply put, a module is a javascript file that has been structured into a smaller, more manageable file size.

Here is a article on MDN Web Docs that goes into greater detail:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

In the example below main.js is a javascript file that is not a module, but both canvas.js and square.js are javascript modules. All 3 are javascript files but canvas and square are broken out into their own files to be more manageable.

Example:
index.html
main.js
modules/
canvas.js
square.js

Image of above example

Here is a link to the code on Github for the example above: https://github.com/mdn/js-examples/tree/master/modules/basic-modules

1 Comment

I like how main.js is not considered a module because it is NOT intended to be reused.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.