Skip to content

QuizNot attempted

Imported bindings are read-only

In index.js: import myCounter from './counter'; myCounter += 1; What happens?
// counter.js
let counter = 10;
export default counter;
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • TypeError: assignment to a constant variable. Imports are read-only live bindings
  • Imported bindings are read-only: assigning to myCounter throws TypeError ('Assignment to constant variable'), whether or not the exporter used let.
  • Not 'becomes 11 only in index.js': importers can't create a local copy by assigning.
  • Not 'counter.js changes too': only the exporting module can change its own variables.
  • Not 'works with a warning': module code is strict, so it throws.
  • Named export let bindings are live
  • export default counter exports the value at that moment.