Exporting values from ES modules in JavaScript

You can export not only functions, but also other values. Let's see how it's done.

Strings export

Let's export a string:

export default 'test';

Let's do the import:

import str from './test.js'; console.log(str);

Array export

We export an array:

export default [1, 2, 3, 4];

We import an array:

import arr from './test.js'; console.log(arr);

Object export

We export an object:

export default { a: 1, b: 2, c: 3 };

We import an object:

import obj from './test.js'; console.log(obj);

Practical tasks

Create a module that exports an array of numbers. Include this module in another file and find the sum of this included array elements.

Create a module that exports three numbers. Include this module in another file and find the sum of these numbers.

enru