Q: How can I get the variables from JSON array in external js file? I got an external JS file which includes JSON array and functions helps me to pick a random set of variables. When I tried to generate the result in the main HTML file, nothing showed in the p tag. Am I mismatching any objects or variables? 
Short Example of HTML file in body section
<p id="result"></p>
<script src="myJSfile.js" type="text/javascript">
<script language="javascript" type="text/javascript">
document.getElementById("result").innerHTML = lottery(selected.name + selected.age);
</script>

Short Example of External JS file
function lottery() {
var from, to, selected=new Array();
var person=
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    if (arguments[0] == "all") {
      from=0;
      to=person.length;
    }
    else {
      from=Math.floor(Math.random()*3);
      to=Math.floor(Math.random()*3+1);
    }
    for (i=0; i<to; i++) {
        selected[i]=person[from];
        from=(from+1)%3;
    }
    return selected;
   }


A: your from, to and selected variables are not visible outside the function just bring this line var from, to, selected=new Array(); out of the function and it should work.
All


var from, to, selected = new Array();
lottery('all');
for(var k = 0; k < selected.length; k++){
  document.getElementById("result").innerHTML += selected[k].name + " " + selected[k].age+'<br />';
}

function lottery() {
var person =
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    
    if (arguments[0] == "all") {
      from = 0;
      to = person.length;
    }
    else {
      from = Math.floor(Math.random()*3);
      to = Math.floor(Math.random()*3+1);
    }
    
    for (i = 0; i < to; i++) {
        selected[i] = person[from];
        from = (from+1)%3;
    }
    
    return selected;
    
   }
<p id="result"></p>


Random


var from, to, selected = new Array();
lottery('all');
for(var k = 0; k < selected.length; k++){
  document.getElementById("result").innerHTML += selected[k].name + " " + selected[k].age+'<br />';
}

function lottery() {
var person =
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    
    if (arguments[0] == "alla") {
      from = 0;
      to = person.length;
    }
    else {
      from = Math.floor(Math.random()*3);
      to = Math.floor(Math.random()*3+1);
    }
    
    for (i = 0; i < to; i++) {
        selected[i] = person[from];
        from = (from+1)%3;
    }
    
    return selected;
    
   }
<p id="result"></p>



A: I have made some changes in your code, please check, this should work
<p id="result"></p>
<script src="stackoverflow.js" type="text/javascript">
<script>
 (function() {
   document.getElementById("result").innerHTML = lottery('all');
 })();
</script>

and in your external js
function lottery() {
var from;
var to; 
var selected=new Array();
var person=
[
 {"name":"Georgia","age":"45"},
 {"name":"John","age":"38"},
 {"name":"Michael","age":"29"},
];
if (arguments[0] == "all") {
  from=0;
  to=person.length;
}
else {
  from=Math.floor(Math.random()*3);
  to=Math.floor(Math.random()*3+1);
}
for (i=0; i<to; i++) {
    selected[i]=person[from];
    from=(from+1)%3;
}
return selected;
}

