JavaScript Modules Example

The following is an example of a JavaScript program that demonstrates the use of modules to organize and reuse code:

In this example, we have two files math.js and main.js. In the math.js file, we have defined two functions add and subtract which takes two arguments and returns the result of the addition and subtraction respectively.

Then in the main.js file, we are importing these two functions from the math.js file using the import statement and then we are calling these functions with some arguments and displaying the result of the function calls on the console.

You can implement this program in any JavaScript development environment that supports the use of modules, such as a text editor with a browser or Node.js.

It's important to note that, modules in javascript are supported natively in modern environments like Node.js and in browser through tools like webpack, babel and others.


No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.