Export and Import Modules
Modules: Export & Import
What is the purpose of export keyword in JavaScript modules?
View Answer:
Here's a simple example. Suppose we have a module that calculates the area of a circle. We can use the export
keyword to make the function available for other modules.
// circle.js
export function area(radius) {
return Math.PI * radius * radius;
}
Here, the area
function is exported from the circle.js
module and can now be imported in another JavaScript file with the import
statement.
What is the purpose of import keyword in JavaScript modules?
View Answer:
Here's a simple example. Suppose you want to use the function area
from the circle.js
module mentioned earlier. You can use the import
keyword to import that function.
// app.js
import { area } from './circle.js';
let r = 5;
console.log(`Area of the circle: ${area(r)}`);
In this app.js
file, the area
function is imported from the circle.js
module, and then it is used to calculate the area of a circle with radius 5.
What are the two types of exports in JavaScript modules?
View Answer:
// mathOperations.js
// Named export
export function add(a, b) {
return a + b;
}
// Default export
export default function multiply(a, b) {
return a * b;
}
In this mathOperations.js
module, add
is a named export and multiply
is a default export. You can have multiple named exports in a module, but only one default export.
Can a module have more than one default export?
View Answer:
How can you import a default export?
View Answer:
// import from 'mathOperations.js'
import multiply from './mathOperations.js';
console.log(multiply(5, 4)); // 20
In this code, the default export (the multiply
function) from the mathOperations.js
module is imported and used to multiply 5 and 4. The name multiply
here can be anything you choose, as it's a default export.
How can you import a named export?
View Answer:
// import from 'mathOperations.js'
import { add } from './mathOperations.js';
console.log(add(5, 4)); // 9
In this code, the named export (the add
function) from the mathOperations.js
module is imported and used to add 5 and 4. The name add
must match the exported name in the module.
Can you import all named exports at once?
View Answer:
// assuming 'mathOperations.js' has multiple named exports
// import from 'mathOperations.js'
import * as mathOps from './mathOperations.js';
console.log(mathOps.add(5, 4)); // 9
console.log(mathOps.subtract(5, 4)); // 1
In this code, all named exports from mathOperations.js
are imported into an object mathOps
. The functions can then be accessed as properties of this object.
Is it possible to use both default and named exports in a module?
View Answer:
Can you rename imports in JavaScript?
View Answer:
// Assuming 'mathOperations.js' has a named export 'add'
// import from 'mathOperations.js'
import { add as addition } from './mathOperations.js';
console.log(addition(5, 4)); // 9
In this code, the named export add
from mathOperations.js
is imported and renamed to addition
. You can now use addition
to refer to the add
function from the mathOperations.js
module.
How does the export keyword work with declarations in JavaScript?
View Answer:
// export an array
export let months = [
'Jan',
'Feb',
'Mar',
'Apr',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
// export a constant
export const MODULES_BECAME_STANDARD_YEAR = 2015;
// export a class
export class User {
constructor(name) {
this.name = name;
}
}
What is the recommendation for exports before functions and class declarations?
View Answer:
export function sayHi(user) {
console.log(`Hello, ${user}!`);
} // no ; at the end
Is there another way to export declarations instead of an explicit export?
View Answer:
// 📁 say.js
function sayHi(user) {
console.log(`Hello, ${user}!`);
}
function sayBye(user) {
console.log(`Bye, ${user}!`);
}
export { sayHi, sayBye }; // a list of exported variables
If you want to import all declarations from a module, what syntax should you use?
View Answer:
// Example 1: Importing everything in say.js
// 📁 main.js
import * as say from './say.js';
say.sayHi('John');
say.sayBye('John');
What is the rationale for explicitly listing all module imports?
View Answer:
// Example 1: Importing everything in say.js
// 📁 main.js
import * as say from './say.js'; // listing everything
say.sayHi('John');
say.sayBye('John');
// Example 2: (Recommended) Only import what we need
// 📁 main.js
import { sayHi } from './say.js'; // explicit list
sayHi('John');
Is it possible to edit or abbreviate the names of our module imports?
View Answer:
// 📁 main.js
import { sayHi as hi, sayBye as bye } from './say.js';
hi('John'); // Hello, John!
bye('John'); // Bye, John!
Can you explain the two main kinds of modules in JavaScript?
View Answer:
What differentiates default and named module exports and imports?
View Answer:
// 📁 user.js
export default class User { // just add "default" to export as default
constructor(name) {
this.name = name;
}
}
// 📁 main.js - importing default User class
import User from './user.js'; // not {User}, just User
new User('John');
////////////////////////////
// No entity names - these are all perfectly valid default exports
export default class { // no class name
constructor() { ... }
}
export default function(user) { // no function name
console.log(`Hello, ${user}!`);
}
// export a single value, without making a variable
export default ['Jan', 'Feb', 'Mar','Apr', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Is it possible to specify the default export without explicitly utilizing it on the default entity (function, class, variable)?
View Answer:
// export a function separately from its definition
function sayHi(user) {
console.log(`Hello, ${user}!`);
}
// same as if we added "export default" before the function
export { sayHi as default }; // <- referencing sayHi
If there is one main default export and a few named ones in your module. How do you import both exported entities?
View Answer:
// 📁 user.js
export default class User {
constructor(name) {
this.name = name;
}
}
export function sayHi(user) {
console.log(`Hello, ${user}!`);
}
// 📁 main.js
import { default as User, sayHi } from './user.js'; // enclosed in curly brackets
new User('John');
Are there any issues with using default exports in JavaScript? Are named exports better?
View Answer:
// Named exports force us to use exactly the right name to import
import { User } from './user.js';
// import {MyUser} won't work, the name must be {User}
// Default exports we can choose the name when importing
import User from './user.js'; // works
import MyUser from './user.js'; // works too
// could be import Anything... and it'll still work
// there’s a rule that imported variables should correspond to file names
import User from './user.js';
import LoginForm from './loginForm.js';
import func from '/path/to/func.js';
What is the purpose of re-exporting a module in JavaScript?
View Answer:
// 📁 auth/index.js
// import login/logout and immediately export them
import { login, logout } from './helpers.js';
export { login, logout };
// import default as User and export it
import User from './user.js';
export { User };
// ...