MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
import & export0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
10.1What are Modules?10.2import & export10.3Organizing Code with Modules
HomeTutorialsJavaScript for Intermediateimport & export
Organizing Code with Modules

import & export

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

The ECMAScript Module (ESM) syntax revolves around two fundamental keywords: export (exposing variables, functions, and classes to external files) and import (consuming those exposed bindings in other files).


1. Named Exports and Imports

Named exports allow you to share multiple identifiers from a single module by name.

Exporting:

1// mathUtils.js
2 
3// Option A: Inline export
4export const PI = 3.14159;
5 
6export function add(a, b) {
7 return a + b;
8}
9 
10export function subtract(a, b) {
11 return a - b;
12}
13 
14// Option B: Grouped export declaration at bottom
15// export { PI, add, subtract };

Importing:

1// app.js
2import { PI, add } from './mathUtils.js';
3 
4console.log(add(10, 5)); // 15
5console.log(PI); // 3.14159

2. Default Exports

Each module can have at most one default export. Default exports are useful when a module's primary purpose is to deliver a single class, component, or configuration:

1// User.js
2export default class User {
3 constructor(name) {
4 this.name = name;
5 }
6}

Importing Default Exports:

When importing a default export, you do not use curly braces {} and can choose any name:

1// consumer.js
2import UserAccount from './User.js'; // Any alias name works!
3 
4const admin = new UserAccount('Sarah');

3. Renaming with the as Keyword

To avoid naming collisions between imported modules, use as to alias identifiers:

1// Renaming during import:
2import { render as renderCanvas } from './canvas.js';
3import { render as renderDOM } from './domRenderer.js';
4 
5renderCanvas();
6renderDOM();
7 
8// Renaming during export:
9const secretKey = 'XYZ_123';
10export { secretKey as apiKey };

4. Namespace Import: import * as

To import all named exports from a module under a single namespace object:

1import * as MathService from './mathUtils.js';
2 
3console.log(MathService.PI);
4console.log(MathService.add(20, 30));
5console.log(MathService.subtract(50, 10));

5. Dynamic Imports: import()

While standard import statements must be static and at the top of the file, the dynamic import('path') function loads modules asynchronously on demand (code-splitting):

1const btn = document.querySelector('#load-editor');
2 
3btn.addEventListener('click', async () => {
4 // Module is fetched over network ONLY when button is clicked!
5 const { RichTextEditor } = await import('./richEditor.js');
6 const editor = new RichTextEditor();
7 editor.mount('#editor-container');
8});

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
How many default exports can a single JavaScript module contain?
Question 2
How do you import a default export from a file named logger.js?
Question 3
What keyword is used to rename an import to avoid name collisions?
Question 4
What does import * as Analytics from './analytics.js' do?
Question 5
What is the primary benefit of dynamic import('./module.js')?
Next Lesson

Organizing Code with Modules

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
What are Modules?Organizing Code with Modules

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext