Your code is ok. And it works fine. Yes, you can use external JS or internal JS.
Using external JS: -
test.html
<html>
<head>
<script type="text/javascript" src="arrayy.js"></script>
</head>
<body>
<input type="button" onclick="go()" value="Display JS Array"/>
</body>
</html>
arrayy.js
function go(){
var array = new Array();
array[0] = "Red";
array[1] = "Blue";
array[3] = "Green";
for (var i=0; i < array.length; i++){
document.write("<li>" + array[i] + "<br />");
}
}
These codes above work in Mozilla Firefox, IE 8 and some other browsers.
Using internal JS: -
<html>
<head>
<script>
function go(){
var array = new Array();
array[0] = "Red";
array[1] = "Blue";
array[3] = "Green";
for (var i=0; i < array.length; i++){
document.write("<li>" + array[i] + "<br />");
}
}
</script>
</head>
<body>
<input type="button" onclick="go()" value="Display JS Array"/>
</body>
</html>
source
share