What are JavaScript modules

JavaScript modules are reusable pieces of code that can be exported from one file and imported into another. This modular structure allows developers to organize their code more effectively, making it easier to maintain and understand. Modules can contain variables, functions, classes, and objects, offering a way to encapsulate functionality and reduce the risk of naming conflicts in larger applications.

With the introduction of ES6 (ECMAScript 2015), JavaScript now supports modules natively via the import and export statements, which streamline the usage of modular code.

// example of exporting a function export function greet(name) { return `Hello, ${name}!`; } // example of importing a function import { greet } from './greet.js'; console.log(greet('World')); // Output: Hello, World!

JavaScript modules ES6 import export reusable code coding practices