How to Export All Functions From a File in JavaScript

  1. Export All Functions From a File in JavaScript
  2. Use the export Function to Export All Functions From a File in JavaScript
How to Export All Functions From a File in JavaScript

In this article, we will learn the purpose and advantage of the export term in JavaScript source code. We will see how we can export the function using JavaScript export term.

Export All Functions From a File in JavaScript

To export functions, objects, and other primitive values from the module, we use the export term in JavaScript so that functions or objects can be used in other JavaScript programs using the import term. If an exported module updates a value, it will also be visible in its imported value.

We can not use the export statement in embedded scripts, and export modules are in strict mode regardless of whether we declare them.

Types of Export:

  1. Named export
  2. Default export

Named Export Basic Syntax

We can add zero or more exports in this type as per module.

export let myVariable = 'hello world!';
export function myFunction() {/* ... */
};

Default Export Basic Syntax

We can add just single default export in this type as per module.

export {myFunction as default};
export default function() {/* ... */
}

Use the export Function to Export All Functions From a File in JavaScript

We just need to use the export statement to export the function in JavaScript, e.g., export function createSum() {}. And to import that exported function in other JavaScript programs, we use the import statement with the correct path, for example, import {createSum} from './firstFile.js.

We can use named export as many as we require in a single file, and we can export multiple functions from a single JavaScript file as well by creating an object of function and use export at once, as shown below:

function function1() {}
function function2() {}
export {function1, function2}  // export with combined as object

Example for the export Function in JavaScript

First of all, we need to create a base file and name it helperFunction.js for a function that we want to export, as shown below:

export function createSum(y, z) {
  return y + z;  // that will return sum
}

As shown above, we have created a helperFunction.js file and declared the createSum() function in it with the export term that will take two values as an argument. We can also use Named Export for the arrow function in modern JavaScript.

Now, we need to import this function to the main JavaScript file and use it as shown below:

import {createSum} from './helperFunction.js';

let value1 = 10;
let value2 = 15;
let result = createSum(value1, value2)

console.log('Sum of two values is : ' + result);

Output:

Sum of two values is : 25

As shown above, we have used the import term to import the helper file function with the correct path in our main file. Then, we initialized two values in the variable.

After that, we called createSum(), passed both values variables as an argument, and stored the return value in the result variable.

Finally, we used the console.log() method to display the sum result in logs. You can create both files, save the above source code, and use the JavaScript compiler to see the result.

Related Article - JavaScript Function