How to add value from function to global array?

I have 3 html and javascript files… for example:

script1.html + script1.js
script2.html + script2.js
show.html + show.js

I’ve connected all .js in .html files:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="show.js"></script>

script1.js:

global = [];
function addvalue1() {global.push("hey")}

script2.js:

function addvalue2() {global.push("hello")}

show.js:

function showvalue() {
    console.log(global[0]);
    console.log(global[1]);
}

addvalue1();
addvalue2()
showvalue();

In console it says:
undefined
undefined

What did I do wrong?

Code works fine for me, so can I see the full html file which contains the following?

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="show.js"></script>

It might be that that the show.js file ends up loading before the other two files? In that case you can add defer attribute to the 3rd script.

1 Like