Suppose we have the following files:
Here app.js is responsible for invoking functions inside script1.js and script2.js and you will get below output in your console window as shown below:
Well, this happens because we have the same variable name in script1.js and script2.js as “greeting”. This is the reason script2.js is overriding over script1.js.
In order to fix this issue, we will use the concept of Immediately Invoked Function Expression (IIFE) as described below:
Now our files will look like this:
In the above two files, we have created IIFE and passed to the window variable and invoked that.
After doing the above mentioned changes that is adding Immediately Invoked Function Expression, the output of the console will look as shown below: