JavaScript export

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

Use export to share values from a module file. Named exports use braces at import time; a module may also have one export default.

JavaScript Syntax

JavaScript
// Imagine math.js
export const PI = 3.14;
export function area(r) { return PI * r * r; }
console.log(area(1).toFixed(2));
Try it Editor

How It Works

  1. Named exports. PI and area are public.
  2. Same file demo. Real projects split files.

Examples

Example 1 — Export list

export { name } at the bottom.

JavaScript
const x = 1; export { x }; console.log(x);
Try it Editor

How It Works

  1. Declares then exports.

Example 2 — Rename on export

export { x as y }.

JavaScript
const x = 2; export { x as y }; console.log(x);
Try it Editor

How It Works

  1. Importers see y.

Example 3 — Default export

One default per module.

JavaScript
export default function main(){ return 1; }
console.log(main());
Try it Editor

How It Works

  1. Default can be anonymous in real files.

Frequently Asked Questions

Yes. Importers use different syntax for each.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful