How to export Variables in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Export Variables in JavaScript #

Use named exports to export multiple variables in JavaScript, e.g. export const A = 'a' and export const B = 'b'. The exported variables can be imported by using a named import as import {A, B} from './another-file.js'. You can have as many named exports as necessary in a file.

Here is an example of exporting variables from a file called another-file.js.

another-file.js
// 👇️ named export export const department = 'web dev'; // 👇️ named export export const tasks = ['develop', 'design', 'test'];

Using export on the same line as the variable's definition is the same as exporting the variables as an object after they have been declared.

another-file.js
const department = 'web dev'; const tasks = ['develop', 'design', 'test']; // 👇️ named exports (same as code snippet above) export {department, tasks};

Here is how we would import the variables in a file called index.js.

index.js
import {department, tasks} from './another-file.js'; console.log(department); // 👉️ 'web dev' console.log(tasks); // 👉️ ['develop', 'design', 'test']

Make sure to correct the path that points to the another-file.js module if you have to. The example assumes that another-file.js and index.js are located in the same directory.

For example, if you were importing from one directory up, you would do import {department, tasks} from '../another-file.js'.

We wrapped the names of the variables in curly braces when importing them. This is called a named import.

The import/export syntax is called ES6 Modules in JavaScript.

In order to be able to import a variable from a different file, it has to be exported using a named or a default export.

The example above uses named exports and named imports.

The main difference between named and default exports and imports is - you can have multiple named exports per file, but you can only have a single default export.

If you try to use multiple default exports in a single file, you would get an error.

another-file.js
const department = 'web dev'; const tasks = ['develop', 'design', 'test']; export default department; // ⛔️ Error. Not allowed to have 2 default exports in a file export default tasks;

IMPORTANT: If you are exporting a variable (or an arrow function) as a default export, you have to declare it on 1 line and export it on the next. You can't declare and default export a variable on the same line.

Having said that, you can use 1 default export and as many named exports as you need in a single file.

Let's look at an example that exports multiple variables and uses both - default and named exports.

another-file.js
const department = 'web dev'; // 👇️ default export export default department; // 👇️ named export export const tasks = ['develop', 'design', 'test'];

And here is how you would import the two variables.

index.js
// 👇️ default and named imports import department, {tasks} from './another-file.js'; console.log(department); // 👉️ 'web dev' console.log(tasks); // 👉️ ['develop', 'design', 'test']

Notice that we didn't wrap the default import in curly braces.

We used a default import to import the department variable and a named import to import the tasks variable.

You can only have a single default export per file, but you can have as many named exports as necessary.

In my experience, most real world codebases exclusively use named exports and imports because they make it easier to leverage your IDE for autocompletion and auto-imports.

You also don't have to think about which members are exported with a default or named export.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.