<b>Open the console to see the output of this code!</b><p>Here, we've <i>assigned</i> the output of the prompt box to the variable called <b>myAge</b>. prompt() asks the user for textual input, and <i>returns</i> a string. confirm() asks the user to select OK or Cancel, and returns a boolean value: true or false. </p>
<p>The keyword <i>var</i> is used when <i>declaring</i> a variable, or defining its existence for the first time in the our computer program. </p>
<p>Chapter 4 of the textbook, p94-99 describes the rules for what types of variable names you can use, and how to capitalize using <i>camelCase</i>. </p>
<p>One informal 'rule; is that the names you choose for variables should make sense when someone (including you!) reads your code. </p>
<p id="demo">
</p>
JavaScript
// prompt for age and output to the console
var myAge = prompt("How old are you?");
console.log(myAge);
// prompt for a different kind of value
var isTruth = confirm("Click OK if what you just entered is true, Cancel if not");
console.log(isTruth);
if (myAge != null) {
document.getElementById("demo").innerHTML =
"Hello, My age is " + myAge + "! How are you today?";
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.