Importing Named Exports
You'll learn how to bring specific named exports from one module into another using the import keyword.
The Core Idea
When a module exports items using named exports, you can selectively import exactly what you need using a special syntax. Think of it like ordering specific items from a menu rather than getting the entire buffet—you list the exact names of what you want, enclosed in curly braces.
The syntax follows this pattern:
import { exportName } from './module.js';
The curly braces {} indicate you're importing named exports (as opposed to default exports, which you haven't learned yet). Inside the braces, you list the exact names of the exports you want to use, matching the names used in the original module's export statements.
Multiple Named Imports
You can import several named exports at once by separating them with commas:
import { name1, name2, name3 } from './module.js';
Example
If a module exports these items:
// math.js
export const PI = 3.14159;
export function square(x) {
return x * x;
}
You can import them like this:
// app.js
import { PI, square } from './math.js';
console.log(PI); // 3.14159
console.log(square(5)); // 25
Notice that the imported names become available in your module scope just like regular variables or functions you've declared locally.
Key Takeaway: Use import { name } from 'module' with curly braces to import specific named exports, listing the exact names you need separated by commas.