Skip to content

JavaScript Modules and Modern Syntax: import, export, Destructuring, ?. and ??

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Until now every example has lived in one script. That is fine for twenty lines, but a real calculator soon has a part that reads the form, a part that does the maths, a part that formats money and a part that talks to an API. Put all of that in one file and every change risks breaking something far away. Modules solve this: each file keeps its own variables private and shares only what it chooses to export. This lesson shows how modules work in the browser, then covers the short modern syntax you will see in almost every module written today: destructuring, default values, spread and rest, optional chaining and nullish coalescing.

Quick answer

A module is a JavaScript file that shares values with export and uses values from other files with import, for example import { addVat } from "./vat.js";. Load the entry file with <script type="module" src="app.js">. Module scripts are deferred, run in strict mode, keep their top-level variables private and must be served over http or https, not opened from a file. Modern syntax that goes with them: const { name, qty = 1 } = item (destructuring with a default), [...a, ...b] and { ...obj } (spread), user?.address?.city (optional chaining) and value ?? 0 (nullish coalescing).

35 min read and practice · Content checked on 15 November 2026

In this lesson you will

  • Split code into modules with named and default exports, and import them
  • Explain how module scripts differ from classic scripts
  • Read and write destructuring, default values, spread and rest
  • Use ?. and ?? to handle missing data, and check if a feature is safe to use

export and import

In a module, nothing is shared unless you say so. Put export in front of a const, a function or a class to make it available: export const VAT_RATE = 0.05; and export function addVat(amount) { ... }. Another file then asks for exactly what it needs by name: import { VAT_RATE, addVat } from "./vat.js";. The names must match, the curly braces are required, and the path starts with ./ or ../ for your own files and includes the .js ending, because the browser fetches it like any other URL.

These are named exports, and a file can have many. A file can also have one default export, written export default function formatAed(n) { ... }, which is imported without braces and under any name you like: import formatAed from "./format.js";. You can rename a named import with as, as in import { addVat as withVat } from "./vat.js";, or take everything at once with import * as vat from "./vat.js"; and then call vat.addVat(100). Most teams prefer named exports, because the names stay the same everywhere and editors can find every use.

Try it: Importing from a second module
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

A single box cannot hold two files, so the second module is packed into a data: URL and loaded with import(). In your own project you would write import formatAed, { addVat } from "./vat.js"; at the top instead. The console lists the exports, with default among them.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between a named export and a default export?

A file can have many named exports, imported in braces with their exact names. It can have only one default export, imported without braces under any name. Named exports keep names consistent across a project, so many teams use only those.

Why do I get a CORS error when I open my module page?

Browsers do not load modules for pages opened straight from your disk. Run a small local server, such as Live Server in Visual Studio Code or npx serve, and open the http://localhost address it gives you.

What is the difference between import and require in JavaScript?

import is the standard module syntax, called ES modules, and works in browsers and in Node.js. require belongs to CommonJS, the older Node.js system. You will still see require in older Node code, but new projects use import.

Do I need a bundler such as Vite or webpack to use modules?

No. Every current browser loads modules on its own. Bundlers become useful in bigger projects: they combine many files into a few for faster loading, let you import packages from npm by name, and reload the page as you edit.

How do I know if a new JavaScript feature is safe to use?

Look the feature up on MDN and read the Baseline badge and the browser compatibility table at the bottom of the page. "Widely available" means it has worked in all major browsers for at least 30 months. For anything newer, test in the browsers your visitors use or provide a fallback.

Sources

Facts in this lesson were checked against these official pages on 15 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.