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

165
Views
Are ES Modules evaluated to their very end before export?

I want be able to export and then modify a object. However I am not sure if this is unexpected behavior.

test.js:

// Short syntax
export const foo = [];
foo[0] = 1;

// Long syntax
const bar = [];
bar[0] = 1;
export { bar };

index.js:

import { foo, bar } from "./test.js";

console.log("foo:" + foo); // Is it guaranteed to be [1]? Or can it be []?
console.log("bar:" + bar); // I always expect this to be [1].

This might be a foolish example but sometimes I have an object and want to register some methods/properties. I want to know if I am able to export it and then register them.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Are ES modules evaluated to their very end before export?

No. In fact, the exports are declared and registered before the module is evaluated.

However, what you actually seem to be asking is

Are ES modules evaluated to their very end before the modules importing them are evaluated?

Yes. test.js will be fully evaluated before index.js is evaluated1. It doesn't matter where in the module you place the import and export declarations.

1: with the exception of circular dependencies. In that case, a module might run before all the imported modules are evaluated, and you may run into uninitialised variables.

Is it guaranteed to be [1]? Or can it be []?

Yes, it's guaranteed to be [1] (or an exception that const foo is not yet initialised). It will never be [], the = [] and the foo[0] = 1 always execute right after each other.

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!