DEV Community


Posted on

How to import a function from a module to the main script?

There is a site with the main script.
To use the import statement we gotta set the type of main script to module.
There is another JS-file with a function.
Let this function return an inverted value.

const inverse = x => 1/x;

export {inverse};
Enter fullscreen mode Exit fullscreen mode

This file is a separate module.
The module exports the inverse function.
In the main script we import the inverse function from the helper module.

import { inverse } from "./helper.js";

Enter fullscreen mode Exit fullscreen mode

Let’s use it and log out the inverted value of 2.
To run the site we need a web-server.
The live-server will do.
Let’s get it started.
So, here is the answer.

I hope you found this article useful, if you need any help please let me know in the comment section.

👋 See you next time. Have a nice day!

Subscribe to our channel:

Discussion (0)