Hello!
I am new to JavaScript. Please forgive my lack of JS vocabulary.
Context
I have three files:
- index.html
- main.js
- sites.js
index.html
<body>
<h1>JavaScript Testing Zone</h1>
<script src="/main.js" type="module"></script>
</body>
main.js
import { sitesMod } from "/sites.js";
sitesMod();
console.log(sites);
sites.js
function sitesMod() {
var sites = [
'https://site1.org/',
'https://site2.org/',
'site3.org/'
];
}
export { sitesMod };
Explanation of code
index.html will run the main.js file.
main.js will import and run the function sitesMod() from sites.js
Problem
console.log(sites); should output https://site1.org/,https://site2.org/,https://site3.org/
instead, console.log(sites); outputs sites is not defined
What I know
I realize that I need to declare something like var sites = X in main.js, but I am unsure how to transfer the content of var sites on sites.js to var sites on main.js
So far using the import and export modules seem to be taking me in the right direction.I need to bridge the final step of transferring the variable's data from one file to another.
I hope I was able to describe my problem in an intelligible way. Please let me know if I can clarify the question. Thank you.
function sitesMod()doesn'treturnanything and running it does not create a global variable calledsites- addreturn sitesto the end of the functions, and dovar sites = sitesMod()in main.js - I would recommend a refresher course on how functions work in javascript