Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Why does importing a script as module change its variable scope?

I couldn't find much documentation explaining this. In JS:

// fileA.js
const varA = 1

// fileB.js
console.log(varA)

In HTML, if it looks like:

<script defer src='./fileA.js'></script>
<script defer src='./fileB.js'></script>

Then it will print '1', but if in HTML, the script tag is changed to

<script defer type='module' src='./fileA.js'></script>
<script defer src='./fileB.js'></script>

Then varA will not be found. Why is this? The workaround is to use window.varA but not sure the logic behind this phenomenon.

Thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!