JavaScript Import and Export
Table of Contents + −
In the previous lesson, we learned that modules let you split code across separate files. Now let’s see exactly how to send code out of one file with export and pull it into another with import.
📦 Named Exports
A named export sends out a value by its name. You add the export keyword in front of a const or a function, and you can export as many names as you like from one file.
// math.jsexport const pi = 3.14159;
export function add(a, b) { return a + b;}Here is what each line in math.js does:
export const pi = 3.14159;creates a constant namedpiand marks it as available to other files.export function add(a, b) {defines a function calledaddand exports it too, so two names leave this file.return a + b;gives back the sum of the two arguments whenaddis called.
To bring those names into another file, you list them inside curly braces. The names inside the braces must match the exported names exactly.
// app.jsimport { add, pi } from "./math.js";
console.log(add(2, 3)); // 5console.log(pi); // 3.14159Here is how app.js uses those imports:
import { add, pi } from "./math.js";pulls both names in from themath.jsfile in the same folder.console.log(add(2, 3));calls the importedaddfunction with2and3, printing5.console.log(pi);prints the importedpiconstant,3.14159.
⭐ Default Exports
A default export sends out one main value from a file. You write export default in front of it, and there can be only one default per file. When you import it, you do not use curly braces, and you can pick any name you want.
// greet.jsexport default function greet(name) { return `Hello, ${name}!`;}Here is what this file exports:
export default function greet(name) {defines a function namedgreetand marks it as the file’s single default export.return `Hello, ${name}!`;returns a greeting string that inserts thenameargument.
// app.jsimport greet from "./greet.js";
console.log(greet("Alex")); // Hello, Alex!Here is how app.js brings in the default export:
import greet from "./greet.js";imports the default value with no curly braces and gives it the local namegreet.console.log(greet("Alex"));calls it with"Alex", printingHello, Alex!.
🔄 Named vs Default Export
Here is how the two kinds of export compare side by side.
| Feature | Named Export | Default Export |
| How many per file | As many as you want | Only one |
| Export syntax | export const add = ... | export default add |
| Import syntax | import { add } from "./math.js" | import add from "./math.js" |
| Curly braces | Required | Not used |
| Name on import | Must match the export | Any name you choose |
🧩 A Two-File Example
Putting it together, one file defines a function and exports it, and the other file imports and uses it. Here is math.js, which exports an add function as a named export.
// math.jsexport function add(a, b) { return a + b;}Here is what math.js does:
export function add(a, b) {defines theaddfunction and exports it as a named export.return a + b;returns the sum of the two arguments to whoever calledadd.
Here is app.js, which imports add and calls it.
// app.jsimport { add } from "./math.js";
const total = add(10, 5);console.log(total); // 15Here is how app.js uses it:
import { add } from "./math.js";pulls the namedaddexport in from the neighboring file.const total = add(10, 5);callsaddwith10and5and stores the result,15, intotal.console.log(total);prints15.
In the browser, the import path is a real file path. It starts with ./ for a file in the same folder, and it includes the .js extension.
The .js extension matters
In the browser, you must write the full file name, including .js. Writing
from "./math" will not find the file. Some bundlers let you drop the
extension, but the browser does not.
🏷️ Renaming a Named Import
Sometimes a name from another file clashes with a name you already have. You can rename a named import with the as keyword to give it a new local name.
// app.jsimport { add as sum } from "./math.js";
console.log(sum(2, 3)); // 5Here is how the rename works:
import { add as sum } from "./math.js";imports the named exportaddbut gives it the local namesum.console.log(sum(2, 3));calls the function through its new namesum, printing5.
Default imports are already flexible
You only need as for named imports. A default import already lets you pick
any name, so renaming is built in.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Mismatched names on a named import | import { Add } when the export is add fails | Match the exported name exactly, or rename with as |
| Using curly braces for a default export | import { greet } for a default export is undefined | Drop the braces: import greet from "./greet.js" |
| Forgetting braces for a named export | import add for a named export does not find it | Wrap named imports in braces: import { add } |
| Wrong relative path | from "math.js" cannot locate the file | Start with ./ and include .js: from "./math.js" |
🔧 Try It Yourself!
- Create a file
math.jsand add a named export:export function add(a, b) { return a + b; }. - Create
app.jsand import it withimport { add } from "./math.js", then calladdand log the result. - Add a second named export to
math.js, such asexport const pi = 3.14159, and import it alongsideadd. - Change one export to a default export with
export default, then update the import to drop the curly braces. - Rename a named import using
asand confirm the new name works.
🧩 What You’ve Learned
- ✅ Named exports use
export constorexport function, and you import them inside curly braces - ✅ A default export uses
export default, and you import it without braces under any name - ✅ Named import names must match the export, but a default import name is your choice
- ✅ In the browser, the import path is relative and includes the
.jsextension - ✅ You can rename a named import with the
askeyword
🚀 What’s Next?
Now that you can split code across files, we will learn how functions can run other functions after they finish. Let’s continue to Callbacks.