Search code examples
javascriptnode.jsmodulecommonjs

Does it have any problem to define same variable name on the multi modules in javascript


I'm trying sublet big single source code to small modules. I want to use same variable name on each jsfile. But them are required on mainjs file.

Does it makes any problem?

main.js

require('./module1');
require('./module2');

module1

let obj = new Something();

module2

let obj = new Something();

Solution

  • Each module is a separate entity and you can use same variables in different modules. But not same variables in a single module. By means of a separate entity is that stack of variables are maintained separately for each module when its complied.