JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to export a function using JavaScript?

A function can be made available to other files by exporting them using the export keyword. There are two ways of exporting a function:

  1. Using named export.
  2. Using default export.

Export a function using named export

In named export, you write the export keyword before the function declaration.

utils.js
export function multiple(x, y){
  return x*y;
}

Here is how you would import the function.

app.js
import {multiply} from './utils.js';

console.log(multiply(4, 5)); //20

Export a function using default export

In default export, you export a function like this:

utils.js
export default function multiple(x, y){
  return x*y;
}

By removing the curly braces, you can import a default exported value.

app.js
import multiply from './utils.js';

console.log(multiply(4, 5)); //20

Recommended Posts