I am facing an issue with two files in my project:
index.js
let list = [];
function add() {
//list.push("item");
list = ["item"];
console.log("B. list length " + list.length);
}
module.exports = {
add,
list
}
test.js
let { add, list } = require('./index');
console.log("A. list length " + list.length);
add();
console.log("C. list length " + list.length);
Current output:
A. list length 0
B. list length 1
C. list length 0
Expected output:
A. list length 0
B. list length 1
C. list length 1
I cannot figure out why the value of list
is not updating in test.js.
The workaround I found is to use list.push("item");
instead of list = ["item"];
.
This behavior is puzzling to me.