+ 1

Undefined when using prompt in a function

why when I use this I get 'ig is not defined'. How should I return values? function askData(){   var ig = prompt("Instagram:");   var fb = prompt("Facebook:");   var em = prompt("Email:"); } askData(); sendData(ig,fb,em);

19th Aug 2018, 2:57 PM
Pablo
Pablo - avatar
5 Answers
+ 3
You would firstly need to get the function to return the variables ig,fb and em so that you can use them outside of the askData() function. function askData(){   var ig = prompt("Instagram:");   var fb = prompt("Facebook:");   var em = prompt("Email:"); return [ig,fb,em]; } var data = askData(); alert(data);
19th Aug 2018, 8:02 PM
Russ
Russ - avatar
+ 6
They are all undefined because the variable declarations are inside the function askData. They have local scope. Declare the variables outside for them to have global scope so you can pass them as arguments to sendData.
19th Aug 2018, 3:07 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 1
No problem. Shame it took so long to get the answer you were looking for! Good luck!
19th Aug 2018, 8:10 PM
Russ
Russ - avatar
0
thank you Jonathan Pizarra what I wanted was to ask for that info inside another function in case the user didnt fill them out. how can I do it?
19th Aug 2018, 3:19 PM
Pablo
Pablo - avatar
0
thanks Russ
19th Aug 2018, 8:07 PM
Pablo
Pablo - avatar