What does export (named, default) mean in JavaScript?

Export in JavaScript is a feature that allows developers to export specific portions of code from a file or module to be used in other files or modules.

When developing a module in JavaScript, the export statement is used to export live bindings to functions, objects, or primitive values from the module.

These data can be used by other modules and apps with the import statement. This is helpful when developers need to access and share code between different files or modules.

Exported modules are in strict mode whether you declare them as such or not.

// Exporting individual features 
export let age = 20, name = 'Tester';

// Export list 
export { 10, 20.50, 'Templates', [0,1,2,3,4] }; 

//Exporting everything at once 
export { object, number, x, y, boolean, string } 

// Renaming exports 
export { variable1 as newName1, variable2 as newName2}; 

//export function
export function functionName(a){
	return a*10;
}

// export funcitons, vriables declared earlier 
export { function1, function2, variable1, variable2 }; 

There are two types of export, named and default. You can have multiple named exports per module but only one default export.

Named exports allow developers to export multiple functions, objects, or variables with a unique name, while default exports allow developers to export a single function, object, or variable as the default export.

export default functionname () { ... } 
export default className { .. }

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close