prompt() with variables

by Lucille Kenney

HTML

<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?";
}