Should the declared Var be running with out me calling it?


I have this typed in:

var userChoice = prompt("Do you choose rock, paper or scissors?");

When I hit 'save and submit code' the prompt pops up and asks me the question I have created. I thought variables don't run unless they are called on... Can anyone clarify this issue?


What exactly do you mean? This:

prompt("Do you choose rock, paper or scissors?");

actually is a function call so you run the function that creates the pop-up.


Ok let's review some definitions so that we both know what we're talking about:

containers for values that are declared by var and to which you can assign a value by variableName = value; and from which you can get the value by using variableName.

Some kind of recipes. Code that is written down somewhere which is not immediately executed and which needs a starting command to run but which can be run multiple times when the starting command is given.

function call:
The previously mentioned starting command. Basically just the appending of () to the variable name in which you've stored the function.

In this case you're not meant to rewrite the prompt function but just to use it. Therefore you call the prompt function by using () after it. The input inside the () matters for the way how it works but not for the fact that it starts the function. That is why you see the pop-up. And when the function call was successful it changes the value of userChoice.


At this point I have only declared the variable and nothing else. If the var statement is the only code on the page then when I submit that code there shouldn't be any happening. Right?


The variable doesn't do anything it just gets a value assigned to it. What is run is the prompt function and that is run by you by appending () to prompt. You didn't build this function right, it is pre built-in into javaScript but you're calling it so when you run the code this function is called. Am I missing something?


What browser are you using. That might affect it @snapdragon101


I am using Google chrome.

I am on build "Rock, Paper, Scissors" lesson, step 2 User Choice. When I click on Hint it tells me to

'Remember to use prompt to ask the user a question, like so:'

and provides the example

'var example = prompt("Question");'

I think this is what is leading me astray.


Yes that is correct. And it is correct that you get the pop-up question. Sry I don't really get the problem.


I really appreciate everyone's help. Thank you haxor for somehow understanding my question. I'm going to smack this code around until I understand it better.

Thank you!


What would be the right way to store the response to my prompt as a variable?

I think I'm going in circles.


You're already doing it right :smile:

prompt is a function
prompt() is a function call
prompt("Question") and prompt() open a pop-up where  "Question" could give the user a helpful message one what to enter in this pop-up text field.

in This pop-up you have a few 2 options:

Press cancel which results in null being returned.
Or entering something in this case you get a string value of your input returned e.g.

entered 42         -> returned value "42"
entered value rock -> returned value "rock"

So what you need now is a variable to store the returned value and that is already what you're doing:

var userChoice = prompt("Do you choose rock, paper or scissors?");