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

219
Views
Why can importing a file let some parts of that file be accessed but not things which aren't exported?

I was following a React tutorial where a javascript file A containing some functions and various console log statements etc is imported into another javascript file B.

One of those functions from file A is then called in file B but there was an error, yet all the console log statements and other commands from file A executed fine.

I wondered why some parts of the file A ran ok but not the functions, doesn't importing the whole file essentially bring everything in that file into scope for file B?

For instance:

a.js

console.log("hi");

function test1() {
  console.log("test1");
}

b.js

import './a.js';

test1();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

...doesn't importing the whole file essentially bring everything in that file into scope for file B?

No, not at all. The example module you've shown doesn't export anything, so everything inside it (the test1 function) is entirely private to the module. This is one of the key benefits of modules: They provide private scope for module details that you don't want to share, with an explicit way to share the things you do want to share.

I wondered why some parts of the file A ran ok but not the functions...

That's because you've imported the module. Even if the module doesn't import or export anything, its top-level code is still run as part of the process of loading it (the first time it's imported). Some modules are designed purely to be loaded for their side-effects (what their top-level code does). Your example is like that; its side effect is the console.log it does.

about 4 years ago · Juan Pablo Isaza Report
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!