JavaScript tutorials > Object-Oriented JavaScript > Encapsulation and Modules > What are JavaScript modules?
What are JavaScript modules?
JavaScript modules are reusable pieces of code that can be imported and exported between different JavaScript files. They provide a way to organize and structure your code, promoting encapsulation and preventing naming conflicts. Before modules, JavaScript code was often written in a global scope, leading to potential problems as projects grew in complexity. Modules solve this by creating private scopes for code and allowing selective exposure of functionalities. Modules improve code maintainability, reusability, and readability. They are a fundamental concept in modern JavaScript development.
Basic Module Syntax: Exporting
The export
keyword is used to make variables, functions, or classes available for use in other modules. There are two main ways to export:
export
before the declaration of a variable, function, or class. You can export multiple named exports from a single module.export default
to export a single value (variable, function, or class) as the default export. A module can only have one default export. The default export is useful when you want to provide a primary or main value from the module.
// my_module.js
// Exporting a single variable or function
export const myVariable = 10;
export function myFunction() {
console.log("Hello from my module!");
}
// Exporting multiple things at once
const anotherVariable = "Some text";
function anotherFunction() {
return "Another function";
}
export {
anotherVariable,
anotherFunction
};
// Exporting a default value
export default function() {
return "Default export";
}
Basic Module Syntax: Importing
The import
keyword is used to bring in variables, functions, or classes from other modules. When importing:
{}
to specify the names of the exported entities you want to import. The names inside the curly braces must match the names used when exporting.defaultValue
in the example).
// main.js
// Importing named exports
import { myVariable, myFunction, anotherVariable, anotherFunction } from './my_module.js';
console.log(myVariable);
myFunction();
console.log(anotherVariable);
console.log(anotherFunction());
// Importing the default export
import defaultValue from './my_module.js';
console.log(defaultValue());
Concepts Behind the Snippet
Modules encapsulate code, preventing naming collisions and promoting reusability. They introduce a clear separation of concerns, making code easier to understand, test, and maintain. Importing and exporting specific parts of a module allows you to control what's exposed and what remains private, enhancing code security and reducing complexity.
Real-Life Use Case
Consider a large web application. You might have a module for handling user authentication, another for making API requests, and a third for managing UI components. Each module encapsulates its specific functionality. For example, the user authentication module might export functions like login
, logout
, and isAuthenticated
. Other parts of the application can import and use these functions without needing to know the internal details of how user authentication is handled.
Best Practices
Interview Tip
When asked about JavaScript modules in an interview, be prepared to explain the benefits of using modules (encapsulation, reusability, maintainability, preventing naming collisions). Be able to demonstrate your understanding of import
and export
syntax, including named and default exports. Also, be prepared to discuss the evolution of modules in JavaScript (CommonJS, AMD, ES Modules) and the advantages of using ES Modules (the standard module system) in modern browsers and Node.js.
When to Use Them
Use JavaScript modules in virtually any modern JavaScript project, especially when:
Memory Footprint
Modules can potentially improve the memory footprint of your application. Because you only import the code you need from a module, you avoid loading and parsing unnecessary code. This is especially true with tree shaking (a technique where unused exports are automatically removed during the build process) which further reduces the bundle size and improves loading times and memory usage.
Alternatives
Before the introduction of ES Modules, there were other module systems used in JavaScript such as: ES Modules are now the standard and preferred approach. CommonJS is still prevalent in Node.js but support for ES Modules is improving.
require()
to import modules and module.exports
to export.define()
to define modules.
Pros
Cons
FAQ
-
What is the difference between named exports and default exports?
Named exports allow you to export multiple values (variables, functions, classes) from a module using specific names. When importing, you must use the same names. Default exports allow you to export a single value as the default export, and you can assign any name you want to it when importing.
-
Do I need a build tool to use JavaScript modules?
For older browsers, yes, you typically need a build tool like Webpack, Parcel, or Rollup to bundle your modules and transpile the code. Modern browsers support ES Modules natively using the
<script type="module">
tag. Node.js also increasingly supports ES Modules but it might require specific configuration or flags. -
Can I use modules in Node.js?
Yes, you can use ES Modules in Node.js. You'll need to make sure you're using a recent version of Node.js and that you've configured your project to use ES Modules. This typically involves setting the
"type": "module"
field in yourpackage.json
file or using the.mjs
file extension for your module files.