Export modules | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Export modules

in the most of the JS libraries you can see a thing export.modules = {} what is this?

5th Nov 2019, 3:00 AM
Roopesh
Roopesh - avatar
1 Answer
+ 3
Its called module, Introduced in 2015 it a statement of core JavaScript, Think of your file structure like this - You have 3 Files inside your directory 1. index.html 2. main.js 3. script.js You write a function like this in your script.js which is not linked to the index.html, and your function is something like this functon whatsmyname(name){ return `Your name is ${name}`; } and you also wanna use this function in main.js, what you wil do........... 1. You will copy the code from file1 to file2 2. You will export the function form script.js and import at main.js and you will link the main.js with your index.html with a attribute type = module, here you are telling your browser that this file is a moudule Here what you will write in your script.js export default functon whatsmyname(name){ return `Your name is ${name}`; } and here how you will import this file to main.js import whatsmyname() from "./script.js"; and now if you will type console.log(whatsmyname("john doe")); then it will return you something like this "Your name is John Doe". ** Some things note here one javascript file can only contain one default module not more then one,If you are importing something file 1 to file 2 you have to link your file 2 with type, module attribute in javascript **
5th Nov 2019, 2:28 PM
Bug Slayer