Question #38

Author: admin
tags: JavaScript  
<script src="main.js" type="module"></script>
<script src="module.js" type="module"></script>
main.js:
import { x, changeX } from "./module.js";

console.log(x); // ??
changeX();
console.log(x); // ??
module.js:
let x = 5;

function changeX() {
  x = 10;
}

export { x, changeX }
What will the console output be?
First 5, then 5.
First 5, then 10.
First 10, then 10.
Nothing, the error will occur: TypeError: Assignment to constant variable.
This is a very curious behavior in Javascript.
Primitives are imported as constants, the values of these variables cannot be changed in the "receiving" file after import, the error TypeError: Assignment to constant variable will occur.
However, a function from a module can change the value of a let variable from its scope. And after that, the value of the constant variable will change in the receiving file!
We can say that the primitive is imported by reference!
Rate the difficulty of the question:
easyhard