Sometimes you are going to want to display a double quote inside a string enclosed with double quotes. This can be done with special characters called escape characters.
document.write("Bart said "I didn't do it"") // this would cause an error
document.write("Bart said \"I didn't do it\"") // this is correct
Click here to see the demoEscape Sequence Character Represented \b backspace \f form feed \n new line \r carriage return \t tab \' single quote \" double quote \\ back slash
Create a report using the tab escape sequence. This demo also uses the new line, quotes, and back slash
Click here to see demo 2Write two lines of text in the alert() function using the carriage return
Click here to see demo 2A variable is a container, it holds data. Remember back in basic math where x=1+2.
The variable x holds the value 3. In JavaScript variables are used to contain data that can
be used or changed later in the program. Declare the variable before assigning data to it by using var.
var x x=1+2 var x,y,z //declare multiple varables at a time var x=1,y=2,z=3 //declare multiple variables and assign a value var x=(1+z) * 5 //declare the variable and use in an expression to assign a value
Some programming languages suggest naming variables by the type of value it will hold.
Last week we learned how to use JavaScript to write to the browser window. You might be thinking "That's Great! But why would I ever want to do that?". Well, lets take some HTML that tells the story about Jack and the Bean Stalk. Now let's use JavaScript and variables to personalize the story with a child's name.
var childName = "Maxamillion"; document.write(childName);We know from last week that we can also write HTML with JavaScript, so we could add some color to the name, along with making the name bold.
var beginColor = '<font color="#FF0000"><b>'; var childName = "Maxamillion"; var endColor = "</b></font>"; document.write(beginColor + childName + endColor);Click here to see the story before JavaScript
These can be integers (no decimals) or floating-point numbers (with decimals).
var pay = 500;
var bonus = pay * .05
var totalPay = pay + bonus
document.write("with my bonus I will have " + totalPay + " dollars");
Click here to see the demoThese are variables set to true or false. Boolean values are most commonly used as flags. You may have created a test and set a variable to true. If the person missed an answer, you could set the variable to false. Then you could easily check if they had gotten all the answers correct.
var allCorrect = true; //start the variable off as true allCorrect = false; //if an answer was wrong, change the variable to false
var novalue = null;