Including these two scripts into an HTML page and loading then in the browser results in the following output:
The text variable of script 2 overwrites the variable from script 1 and we get the same message printed twice. Remember there is no file scope so both text variables are added to the global scope and the because they use the same name the second overwrites the first.
Fixing the problem basically means wrapping all the code in each file in a function. As we don’t actually need to reference the function anywhere else this is typically done with an Immediately-Invoked Function Expression (IIFE). See the code below:
Now the both text variables are scoped to the function they are contained in and no longer conflict. See the output below: