- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Exports & Imports in JavaScript
Note − To run this example you will need to run a localhost server.
Following is the code for exports and imports in JavaScript −
INDEX.html
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; } </style> </head> <body> <h1>Exports and imports in JavaScript</h1> <button class="Btn">IMPORT</button> <div class="result"></div> <h3>Click on the above button to import module</h3> <script src="script.js" type="module"></script> <script src="sample.js" type="module"></script> </body> </html>
script.js
import test from './sample.js'; document.querySelector('.Btn').addEventListener('click',()=>{ test(); })
sample.js
let resultEle = document.querySelector(".result"); export default function testImport(){ resultEle.innerHTML = 'Module testImport has been imported'; }
Output
On clicking the ‘IMPORT’ button −
- Related Articles
- Renaming imports and exports in JavaScript
- Default exports vs Named exports in JavaScript.
- Dynamic imports in JavaScript.
- Using ‘{ }' in JavaScript imports?
- Awaiting on dynamic imports in JavaScript.
- Absolute and Relative Imports in Python
- What are static imports in java? Example.
- How to do multiple imports in Python?
- Package Imports in JShell of Java 9
- Deviations in two JavaScript arrays in JavaScript
- Keeping only alphanumerals in a JavaScript string in JavaScript
- Node in Javascript
- Number.NEGATIVE_INFINITY in JavaScript
- Number.POSITIVE_INFINITY in JavaScript
- Object.assign() in JavaScript?

Advertisements