scope

am a new to this world, i hope you understand.

am working on a code where the user inputs some numbers, i put them in an array, am having trouble accessing that array.
EG

function getnum(){
var num=[];
num.push(prompt(enter 3 numbers));
document.write (num); // this prints out.
};

document.write (num);// i cannot access this array

You need to move the num array outside of the function as follows:

var num;

function getnum(){
   num = [];
   num.push(prompt(enter 3 numbers));
   document.write (num); // this prints out.
};

document.write (num); // now it works!

ahh, man I am so slow :slight_smile: Kirupa beat me to it…

I guess num is declared inside the function getnum and not accessible for document?

var num=[]; //move here    

function getnum(){
    num.push(prompt(enter 3 numbers));
    document.write (num); // this prints out.
    };

  document.write (num);// i cannot access this array