How would I use a var from inside a function, outside of that function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How would I use a var from inside a function, outside of that function?

For instance, how would I get this short script to call the saved value from the variable "name" inside the function 'askName' and output the result inside the function 'calculateTwoNumbers'? function askName() { var name = prompt("What is your name?"); if (name === null) { alert("Why did you cancel?"); askName(); } else if (name === "") { alert("You didn\'t enter anything."); askName(); } else { alert("Hello, " + name + ("! How are you?")); return name; } } function calculateTwoNumbers() { alert("Well, " + name + ". Next step is calculating...") var num1 = +prompt("What is the first number you want to calculate?"); } askName(); calculateTwoNumbers(); Thank you.

22nd Jul 2017, 12:02 AM
Danj Wood
Danj Wood - avatar
10 Answers
+ 3
The key concept here is the 'variable scope'... When you define functions, you declare a block of instructions wich define a scope for variables created inside: function myFunc() { var myLocalVar = 42; alert(myLocalVar); // output 42 } ... trying to access them outside of the myfunc() scope, will fail: the variable is hidden from outside the scope: function myFunc() { var myLocalVar = 42; alert('myLocalVar = '+myLocalVar); // output "myLocalVar = 42" } myFunc(); // execute myFunc block of code, so output 42 alert('myLocalVar = '+myLocalVar); // output "myLocalVar = undefined" In the same way, myLocalVar isn't visible from another function defined outside of myFunc() ^^ Now, defining a variable outside of a local scope define it in an implicit 'global' scope: var myGlobalVar = 42; function myFunc() { alert(myGlobalVar); // output 42 because myFunc() and myGlobalVar share the same scope } function mySecondFunc() { alert(myGlobalVar); // output 42 because mySecondFunc() share also the same scope } function myThirdFunc() { var myGlobalVar = -1; // the 'var' keyword define an explicit local scope var, which will hide the same named global scope var, without changing it's value alert(myGlobalVar); // so output -1 } myThirdFunc(); // call third function, so output -1, but... alert(myGlobalVar); // still output 42, as... myFunc(); // also still output 42 As previously explained by @Tiago Soares, the other way to export variables from their local scope is to return values used/calculated ;)
22nd Jul 2017, 8:46 AM
visph
visph - avatar
+ 2
In function askName () return the value of name variable. Then inside the function calculateTwoNumbers () you call the function askName (). For example: If you need the value only for alert, use: alert (askName ()); or if you need the value for a variable: var anything = askName ();
22nd Jul 2017, 12:15 AM
Tiago Soares
Tiago Soares - avatar
+ 1
I adapted your code. Please see the comments I wrote, and the result of the code https://code.sololearn.com/WAY1a42ekyWD/?ref=app
22nd Jul 2017, 12:42 AM
Tiago Soares
Tiago Soares - avatar
+ 1
Thank you Tiago, I think I understand now!
22nd Jul 2017, 12:47 AM
Danj Wood
Danj Wood - avatar
+ 1
You have to notice two things: 1) alert () is a function that displays the text/value (it can be a text inside quotes or a variable) you provide inside its parentheses (i.e parameters) 2) As the function askName() returns a value, it works like a variable. Is a little difficult? Let's see these examples Example I: function askName (){ var name = "Test"; return name; } askName (); //Does not display anything, the computer only created name variable assigned as "Test" Example II: function askName (){ var name = "Test"; return name; } function foo { var otherName = askName (); alert (otherName); } foo (); //Displays "Test" Example III: function askName (){ var name = "Test"; return name; } alert (askName ()); //Displays "Test"
22nd Jul 2017, 1:24 AM
Tiago Soares
Tiago Soares - avatar
+ 1
Thank you Tiago, very clear and easy to understand answer, much appreciated. I'll keep playing with functions and variables and hopefully get the hang of them, as they say, practice makes perfect!
22nd Jul 2017, 2:21 AM
Danj Wood
Danj Wood - avatar
0
How would I return the value of the name variable? I've tried return askName(); but it just loops the askName function? Thank you.
22nd Jul 2017, 12:25 AM
Danj Wood
Danj Wood - avatar
0
This is working: function askName() { var name = prompt("What is your name?"); if (name === null) { alert("Why did you cancel?"); askName(); } else if (name === "") { alert("You didn\'t enter anything."); askName(); } else { alert("Hello, " + name + ("! How are you?")); return name; } } function calculateTwoNumbers() { alert("Well, " + askName() + ". Next step is calculating...") var num1 = +prompt("What is the first number you want to calculate?"); } calculateTwoNumbers(); But I don't understand why? How is it running the function 'askName', when the end of the code is only calling for calculateTwoNumbers, I apologize for the "noob" questions, I'm obviously overlooking something here.
22nd Jul 2017, 1:00 AM
Danj Wood
Danj Wood - avatar
0
Inside calculateTwoNumbers () you have an alert: alert ("Well, " + askName () + ".Next step is calculating...") The askName () is calling the askName () function No problem for asking, you learn from it
22nd Jul 2017, 1:05 AM
Tiago Soares
Tiago Soares - avatar
0
I see, so even inputting a function name inside an alert or prompt, will call that function? I thought it had to be a separate statement to call the function, I guess I have a lot more research to be doing...
22nd Jul 2017, 1:10 AM
Danj Wood
Danj Wood - avatar