Skip to content
FrontDepth, Home

Back to the topic

Practice: ES modules

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

4 predict-output · 3 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    Four small modules are built as data: URLs (each string is a real module). main imports a and b, and both import shared. What does this code log?

    JavaScript
    const mod = (source) => "data:text/javascript," + encodeURIComponent(source);
    
    const shared = mod(`console.log("shared");`);
    const a = mod(`import "${shared}"; console.log("a");`);
    const b = mod(`import "${shared}"; console.log("b");`);
    const main = mod(`import "${a}"; import "${b}"; console.log("main");`);
    
    console.log("before import");
    await import(main);
    console.log("after import");
  2. Open questionJunior

    How is an ES module different from a classic script?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A counter module is imported dynamically, its export is changed from inside and then from outside. What does this code log?

    JavaScript
    const mod = (source) => "data:text/javascript," + encodeURIComponent(source);
    
    const counter = mod(`
      export let count = 0;
      export function increment() {
        count += 1;
      }
    `);
    
    const m = await import(counter);
    console.log(m.count);
    m.increment();
    console.log(m.count);
    
    try {
      m.count = 100;
    } catch (error) {
      console.log(error.name);
    }
    console.log(m.count);
    
    const again = await import(counter);
    console.log(again === m);
  4. Predict the outputMiddle

    One variable is exported three ways: as a named export, as the default export and under an alias. After update(), what does each one show?

    JavaScript
    const mod = (source) => "data:text/javascript," + encodeURIComponent(source);
    
    const m = await import(mod(`
      export let value = 1;
      export default value;
      export { value as alias };
      export function update() {
        value = 2;
      }
    `));
    
    m.update();
    console.log(m.value);
    console.log(m.default);
    console.log(m.alias);
  5. Open questionMiddle

    Why can bundlers tree-shake ES modules much better than CommonJS, and what still prevents tree shaking in ES modules?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Predict the outputSenior

    app imports config, which uses top-level await, and logger, which does not. In what order are the lines logged?

    JavaScript
    const mod = (source) => "data:text/javascript," + encodeURIComponent(source);
    
    const config = mod(`
      console.log("config start");
      await null;
      console.log("config end");
      export const ready = true;
    `);
    const logger = mod(`console.log("logger");`);
    const app = mod(`
      import { ready } from "${config}";
      import "${logger}";
      console.log("app " + ready);
    `);
    
    await import(app);
    console.log("done");
  7. Open questionSenior

    How do ES modules and CommonJS work together in Node.js today, and what are the limits?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.