Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What is spread, default and rest parameters in JavaScript ?
Next article icon

What is export default in JavaScript ?

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript modules allow you to organize code into separate files, making it easier to maintain and reuse. To share code between modules, JavaScript provides two types of exports: Named Exports and Default Exports. Understanding how and when to use these export types is key to effectively managing modular code.

1. Named Exports

Named exports are used when you want to export multiple values from a module. Each exported value must be imported by its exact name, which enforces consistency and clarity in your code.

Syntax:

// Exporting individual features

export let name1 = …, name2 = …, …, nameN; // also var, const

// Export list

export { name1, name2, …, nameN };

//Exporting everything at once

export { object, number, x, y, boolean, string }

// Renaming exports

export { variable1 as name1, variable2 as name2, …, nameN };

// export features declared earlier

export { myFunction, myVariable };

Example: In this example, we are exporting everything by their default name.

javascript
//file math.js function square(x) {     return x * x; } function cube(x) {     return x * x * x; } export { square, cube };   //while importing square function in test.js import { square, cube } from './math; console.log(square(8)) //64 console.log(cube(8)) //512 

Output:

64
512

Default Exports

Default exports are used when you want to export a single primary object, function, or variable from a module. This type of export allows you to import the value using any name, providing flexibility and simplifying the import process for the module’s main content.

Example: In this example, we are exporting the variable by using “export default” keywords.

javascript
// file module.js let x = 4; export default x;  // test.js // while importing x in test.js import y from './module'; // note that y is used import x instead of  // import x, because x was default export console.log(y); // output will be 4 

Output:

4

Using Named and Default Exports at the same time

JavaScript allows you to use both named and default exports in the same module. This flexibility is helpful when you have a primary export along with additional supporting exports.

Example: In this example, we are exporting the function.

javascript
//module.js let x = 2; const y = 4; function fun() { 	return "This a default export." } function square(x) { 	return x * x; } export { fun as default, x, y, square }; 

While importing this module.js we can use any name for fun because it is a default export and curly braces for other named exports. 

javascript
//test.js file import anyname, { x, y, square} from './module.js'; console.log(anyname()); //This is a default export. console.log(x); //2 

Output:

This is a default export.
2


Next Article
What is spread, default and rest parameters in JavaScript ?
author
sanketpathak64
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Misc

Similar Reads

  • What is Internal and External JavaScript?
    Internal and External JavaScript are the two ways of adding JavaScript code to an HTML document. External JavaScript refers to adding JavaScript code in HTML from a separate .js file using the src attribute of <script> tag while the Internal JavaScript is the JavaScript code embedded within th
    2 min read
  • Difference Between Default & Named Exports in JavaScript
    In JavaScript, exports allow you to share code between modules. There are two main types: default exports and named exports. Used to export functions, objects, or variables.Default exports allow importing with any name.Named exports require importing by the exact name.Named ExportsNamed exports let
    4 min read
  • Explain about IIFEs in JavaScript
    In this article, you will learn about JavaScript immediately invoked function expressions (IIFE). A JavaScript immediately Invoked Function Expression is a function defined as an expression and executed immediately after creation. The following shows the syntax of defining an immediately invoked fun
    3 min read
  • What is spread, default and rest parameters in JavaScript ?
    The default, spread, and rest parameters were added in ES6. Default Parameter: It is used to give the default values to the arguments, if no parameter is provided in the function call. Syntax: function fnName(param1 = defaultValue1, ..., paramN = defaultValueN) { ... } Example 1: In the below exampl
    2 min read
  • Is There an Equivalent for var_dump (PHP) in JavaScript?
    In JavaScript, there isn't an exact equivalent of PHP's var_dump(), which displays detailed information about a variable, including its type and value, in a structured way. However, there are several ways to inspect variables and objects, providing similar functionality. Here are some of the common
    3 min read
  • JavaScript Importing and Exporting Modules
    JavaScript Modules are basically libraries which are included in the given program. They are used for connecting two JavaScript programs together to call the functions written in one program without writing the body of the functions itself in another program. Importing a library: It means include a
    3 min read
  • JavaScript Object defineProperties() Method
    The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object. Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are
    2 min read
  • Default Constructor in JavaScript
    In JavaScript, a default constructor is not explicitly defined like in some other programming languages such as Java or C++. In JavaScript, objects can be created without a formal constructor. When you create an object using the new keyword along with a constructor function, that function serves as
    2 min read
  • How to write a function in JavaScript ?
    JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
    4 min read
  • How to share code between files in JavaScript ?
    JavaScript is a powerful and popular programming language that is widely used for web development. One of the key features of JavaScript is its ability to share code between files. This can be useful for organizing large projects, reusing code, and maintaining code quality. In this article, we'll se
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences