Next freebie in 28 Hours*
prompt() accepts two string parameters. The first one is the message which will be displayed on the pop-up box like “How Old Are You?” while the second argument is what should be displayed in the text box where the user will input the data. This argument is usually left blank unless you want users to have a certain default string so they can just click OK.
What will "prompt" return to us? Whatever the user enters in the prompt box will be returned as a string. Remember "prompt" only returns a string, even if the user enters 23. "23" will be stored in a a string and cannot be used as a number. Here it is in action:
var userAge = prompt(“Enter your age”, “ ”);
Let’s look at this string returning in detail. We have a variable var num1 which has 10 stored in it: we will ask the user for a number which we can add to variable num1. We use "prompt" and ask the user for the second number, a pop up box is displayed with the message “Please Enter a Number”: the user types 10, which we store in another variable called var num2. Now we display the result using "alert" like this:
alert(num1 + num2);
What do you think we will get?
Now when we call "alert" like this:
alert(parsetFloat(num1) + parseFloat(num2));
ParseFloat(); can also convert to decimal values. If we want an integer there is parseInt(); which returns only integer values. We can additionally perform all the basic math functions on variables such as num1/num2, num1*num2, num1-num2. Play around with this and you'll quickly learn it.
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.