Modules
Modules divide large programs into separate files based on tasks, grouping related functions, variables, or classes for easier management and reuse.
Suppose a file named greetPerson.js contains the following code:
// Exporting a functionexport function greetPerson(name) {return `Hi, ${name}`;}
Now, to use the code of greetPerson.js in another file main.js, the following code can added to the other file:
// Importing greetPerson from greetPerson.js fileimport { greetPerson } from './greetPerson.js';// Using greetPerson() defined in greetPerson.jslet displayName = greetPerson('Codecademy');console.log(displayName);// Output: Hi, Codecademy
Two things happened:
In the greetPerson.js file, the
greetPerson()
function is exported using theexport
keyword.export function greetPerson(name) {...}In the main.js file, the
greetPerson()
function is imported using theimport
keyword.import { greetPerson } from '/.greetPerson.js';
Note that there are {
}
wrapped around the function in the import syntax.
Export Multiple Objects
It is also possible to export multiple objects from a module.
For example, suppose there’s a module.js file:
// Exporting the variableexport const name = 'Codecademy Docs';// Exporting the functionexport function difference(x, y) {return x - y;}
In main file main.js:
import { name, difference } from './module.js';console.log(name); // Output: Codecademy Docslet diff = difference(9, 5);console.log(diff); // Output: 4
Here, both the name
variable and the difference()
function from the module.js file are imported to main.js.
Renaming Imports and Exports
To prevent naming conflicts, it is important to rename variables or functions when exporting or importing. This ensures the program uses the correct values from the intended file.
Rename in the export file (the module)
// In module.jsexport { function1 as newName1, function2 as newName2 };
// In main.jsimport { newName1, newName2 } from './module.js';
When exporting from module.js, new names (newName1
, newName2
) are assigned to functions, the new names must be used when importing and referencing them.
Rename in the import file (the main file)
// In module.jsexport { function1, function2 };
// In main.jsimport { function1 as newName1, function2 as newName2 } from './module.js';
When importing the function, new names (newName1
, newName2
) are used. These new names are then used to reference the functions in the code.
Default Export
You can also perform default export of the module. In the file greetPerson.js:
// Default exportexport default function greetPerson(name) {return `Hi, ${name}`;}export const age = 23;
Then when importing, you can use:
import random_name from './greetPerson.js';
While performing default export,
random_name
is imported from greetPerson.js. Sincerandom_name
is not in greetPerson.js, the default export (greetPerson()
in this case) is exported asrandom_name
.- You can directly use the default export without enclosing curly brackets
{}
.
Modules Always use Strict Mode
By default, modules are in strict mode. For example:
// In greetPerson.jsfunction greetPerson() {// Strict by default}export greetPerson();
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours