Updated April 15, 2023
Introduction to JavaScript export module
The live bindings to the objects, primitive values, and functions can be exported from a module by making use of the export statement while creating the modules of JavaScript. The other programs can then use it by making use of import statement. Thus, the modification of the exported bindings is possible locally, whereas the modification of the imported module is not possible, and the imported module is read-only. However, when there are changes in the exported module, the same changes are reflected in the imported module, and the exported statements are not allowed to be used in the scripts that are embedded.
Syntax
export { name1, name2, …, nameN };
export expression;
export function;
where,
name1, name2..,nameN is the items in the list,
the expression is the expression to be exported and
the function is the function to be exported.
Working of JavaScript export module
- Whenever there is a need to call the functions written in one program from another program without writing the entire function in another program, we use modules in JavaScript.
- The live bindings to the objects, primitive values, and functions can be exported from a module by making use of the export statement while creating the modules of JavaScript, which the other programs can then use by making use of the import statement.
- The modification of the exported bindings is possible locally, whereas the modification of the imported module is not possible, and the imported module is read-only.
- However, when there are changes in the exported module, the same changes are reflected in the imported module.
- The exported statements are not allowed to be used in the scripts that are embedded.
Examples
Different examples are mentioned below:
Example #1
JavaScript program to demonstrate export module to perform addition of the given two numbers:
Contents in the first file: script.js
//module.exports statement represents that the function can be exported into any other file by making use of the file name. The function sum is defined here which accepts two numbers as input and performs addition of the two numbers and returns the result.
module.exports = sum (firstNumber, secondNumber)
{
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
this.sumoftwonos = sum ()
{
return this.firstNumber + this.secondNumber;
}
}
Contents in the first file: script1.js
//we make use of require function to provide the file name consisting of the functions that needs to be imported and is assigned to a variable
var script = require('./script.js');
//The function is then called while passing the parameters to the function and the result returned is stored in a variable and displayed as the output on the screen
var script1 = new sum(10,20);
console.log(script1.sumoftwonos());
The output of the program is as shown in the snapshot below:
There are two JavaScript files, one consisting of the body of the function and the other file using the function by making use of the export statement. In the first JavaScript file, module.Export statement represents that the function can be exported into any other file by using the file name. The function sum is defined here, which accepts two numbers as input and performs the addition of the two numbers and returns the result. In the second JavaScript file, we use the required function to provide the file name consisting of the functions that need to be imported and assigned to a variable. The function is then called while passing the parameters to the function, and the result returned is stored in a variable and displayed as the output on the screen.
Example #2
JavaScript program to demonstrate the export module to perform multiplication of the given two numbers:
Contents in the first file: script.js
//module.exports statement represents that the function can be exported into any other file by making use of the file name. The function sum is defined here which accepts two numbers as input and performs addition of the two numbers and returns the result.
module.exports = mul (firstNumber, secondNumber)
{
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
this.prodoftwonos = mul ()
{
return this.firstNumber * this.secondNumber;
}
}
Contents in the first file: script1.js
//we make use of require function to provide the file name consisting of the functions that needs to be imported and is assigned to a variable
var script = require('./script.js');
//The function is then called while passing the parameters to the function and the result returned is stored in a variable and displayed as the output on the screen
var script1 = new mul(5,6);
console.log(script1.prodoftwonos());
The output of the program is as shown in the snapshot below:
There are two JavaScript files, one consisting of the body of the function and the other file using the function by making use of the export statement. In the first JavaScript file, module.Export statement represents that the function can be exported into any other file by using the file name. For example, the function mul is defined here, which accepts two numbers as input and performs multiplication of the two numbers and returns the result. In the second JavaScript file, we use the required function to provide the file name consisting of the functions that need to be imported and assigned to a variable. The function is then called while passing the parameters to the function, and the result returned is stored in a variable and displayed as the output on the screen.
There are several advantages of using an export module in JavaScript. Some of them are:
- By making use of the export module in JavaScript, there is no necessity to force all the functions to be in the same file.
- The functions can be made reusable and the public by making use of export modules in our scripts.
Conclusion
In this tutorial, we understand the concept of the export module in JavaScript through definition, syntax, working of export module in JavaScript through programming examples, and their outputs and advantages.
Recommended Articles
We hope that this EDUCBA information on “JavaScript export module” was beneficial to you. You can view EDUCBA’s recommended articles for more information.