Welcome!
I am just starting to learn JavaScript. Excuse me if I don't know all the technical terms yet.
Scenario
Currently, my project consists of three main files:
- index.html
- main.js
- sites.js
index.html
<body>
<h1>JavaScript Playground</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 };
Code Explanation
index.html is responsible for running the main.js script.
main.js imports and executes the sitesMod() function from sites.js.
Issue
The expected output of console.log(sites);
should be
https://site1.org/, https://site2.org/, site3.org/
.
However, instead of the desired result, console.log(sites);
displays sites is not defined
.
Current Understanding
I understand that I might need to declare something like var sites = X
in main.js, but I'm unsure how to transfer the data stored in var sites
in sites.js to var sites
in main.js.
Although utilizing the import/export modules seem promising, I'm struggling with the final step of transferring the variable content between files.
I hope this explanation sheds light on my issue. Please feel free to ask for further clarification. Thank you.