The Wayback Machine - https://web.archive.org/web/20240126171425/https://www.geeksforgeeks.org/what-is-export-default-in-javascript/
Open In App
Related Articles

What is export default in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The export statement is used when creating JavaScript modules to export objects, functions, and variables from the module so they can be used by other programs with the help of the import statements. There are two types of exports. One is Named Exports and the other is Default Exports. 

Named Exports

Named exports are useful for exporting several values. During the import, it is mandatory to use the same name as the corresponding object. 

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 useful to export only a single object, function, variable. During the import, we can use any name to import. 

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

It is possible to use Named and Default exports in the same file. It means both will be imported in the same file.

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 13 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials