मॉड्यूल्स ES 2015 में पेश किए गए थे। मॉड्यूल्स को कोड को छोटे टुकड़ों में तोड़ने के लिए पेश किया गया था। मॉड्यूल्स में क्लासेज या फ़ंक्शन्स हो सकते हैं। वेरिएबल्स, फ़ंक्शन्स, ऑब्जेक्ट्स को एक्सपोर्ट और इम्पोर्ट करने के लिए export और import कीवर्ड्स का उपयोग किया जाता है।
नोट - इस उदाहरण को चलाने के लिए आपको एक लोकलहोस्ट सर्वर चलाने की आवश्यकता होगी।
जावास्क्रिप्ट में मॉड्यूल्स का कोड निम्नलिखित है
INDEX.html
उदाहरण
<!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:blueviolet; } </style> </head> <body> <h1>जावास्क्रिप्ट मॉड्यूल्स</h1> <button class="Btn">IMPORT</button> <div class="result"></div> <h3>मॉड्यूल इम्पोर्ट करने के लिए ऊपर दिए गए बटन पर क्लिक करें</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'; }
आउटपुट

'IMPORT' बटन पर क्लिक करने पर -
