. Advertisement .
..3..
. Advertisement .
..4..
A Javascript program can be separated into various files. Import and export directives offer a wide range of syntax variants. So how to export a function in Javascript? How to make a defined function from a file available to others?
This tutorial will give detailed answers for these questions.
How To Export A Function In Javascript
One of the best methods to export a function in Javascript is using named exports. For instance, you can use `import {sum} from ‘./another-file.js’ to import an exported function.
Let’s have a look at the following example of function exporting process from a another-file.js file:
// 👇️ named export
export function sum(a, b) {
return a + b;
}
// 👇️ named export (arrow function)
// export const multiply = (a, b) => {
// return a * b;
// };
In this example, when the `export` and the function’s definition are used on this line, it works the same way as the function exporting process as a declared object.
Here is the command for importing this function in the `index.js` file:
import {sum} from './another-file.js';
console.log(sum(45, 55)); // 👉️ 100
Ensure that you use the correct path of the `another-file.js` module. In the above example, both the index.js and another-file.js are located in this directory.
It is worth noting that to import a different file’s function, you need to export it using a default or named export. The only difference between these two types of exports is their number.
There is a single default export while you can get various named exports in each file. For this reason, if you execute any default export in a file, an error will surely occur.
How To Export Multiple Functions In Javascript
If you want to export various variables and functions, the same export statements can be used to accomplish the task. For example:
export const firstname = "Alice";
export const lastname = "Smith";
You can also execute the export statement before the function definitions. This way, the function will be exported automatically and there is no need to use a separate export statement.
How To Import Multiple Functions
Besides export methods, you should also prepare yourself with some ways to import functions. To do this practice in Javascript, let’s specify the object with the imported function name.
For example, if you want to export fun1 and fun2 functions from the example.js file, import them into another destination in the same file with the code as follows:
import { fun1, fun2 } from "./example"
Here, you don’t have to use the file path’s .js extension. Don’t forget to change the imported path if you locate the module in another folder.
Conclusion
Exporting a function in Javascript allows users to use it in other files of your application. Without a shade of doubt, modules serve multiple purposes, including providing simple utilities for string modifications.
Check the article again to get the best method to export one or more functions in Javascript.
Leave a comment