
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Default exports vs Named exports in JavaScript.
There can be only one default export per file and we can assign it any name when importing it to the other file. However, there can be multiple named exports in a file and they are imported using the name that were used to export.
Following is the code showing difference between default exports and named exports in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Default export vs Named Export</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above export function as named and default export</h3> <script src="script.js" type="module"> </script> </body> </html>
sample.js
export default function testImport(){ return 'Module testImport has been imported'+''; } export function tellTime(){ return new Date(); }
script.js
import defaultExport from "./sample.js"; import {tellTime} from "./sample.js"; let resultEle = document.querySelector('.result'); document.querySelector('.Btn').addEventListener('click',()=>{ resultEle.innerHTML+=defaultExport(); resultEle.innerHTML+=tellTime(); })
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Exports & Imports in JavaScript
- Renaming imports and exports in JavaScript
- Named arguments in JavaScript.
- Named function expression in JavaScript
- Docker named volumes Vs DOC (data-only-containers)
- Default virtual behavior in C++ vs Java
- How to set named cookies in JavaScript?
- Named capture groups JavaScript Regular Expressions
- How to use named arguments in JavaScript functions?
- Default method vs static method in an interface in Java?
- What are Default parameters in JavaScript?
- What are default function parameters in JavaScript?
- What is a default constructor in JavaScript?
- ES6 Default Parameters in nested objects – JavaScript
- d vs D in JavaScript?

Advertisements