How to change variables across different modules or js files?

I am in the process of seperating my code so it is more clean. I have started doing this by putting my functions in their respective module. So instead of keeping it in the "index.js", I am keeping it in "change.js" (example name). Here is my function:

let number = 5;

function change(){
    number = number + 1;
}

The problem is, when I put this function in its own respective module called "change.js", when it is called, it gives the error that the variable "number" is not declared. If I then declare it in the "change.js" file, the variable "number" only changes in that file and not in "index.js". Is there anyway to change it in both files, or transfer the value from "change.js" to "index.js"?

11 thoughts on “How to change variables across different modules or js files?”

Leave a Comment