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:
src/test.jsexport default 'test';
Let's do the import:
src/index.jsimport str from './test.js';
console.log(str);
Array export
We export an array:
src/test.jsexport default [1, 2, 3, 4];
We import an array:
src/index.jsimport arr from './test.js';
console.log(arr);
Object export
We export an object:
src/test.jsexport default {
a: 1,
b: 2,
c: 3
};
We import an object:
src/index.jsimport obj from './test.js';
console.log(obj);
Practical tasks
Задание 1
Create a module that exports an array of numbers. Include this module in another file and find the sum of this included array elements.
Задание 2
Create a module that exports three numbers. Include this module in another file and find the sum of these numbers.