Working with Data in JavaScript

Using data is basic to nearly any JavaScript program, and JavaScript supports quite a number of different data types: numbers , Boolean values, text strings, and so on. You store data values in variables in JavaScript.

As with other programming languages, variables are simply named locations in memory that you use to store data. You create variables in JavaScript with the var statement, and when a variable has been created, it's ready for you to store and retrieve data in.

Here's an example. In this case, I'm creating a new variable named temperature and storing a value of 72 in it using the = assignment operator. When I use this variable in code, JavaScript replaces it with the value 72 , so I can display the temperature like this:

Listing ch06_04.html
 <HTML>     <HEAD>         <TITLE>             Using Variables in JavaScript         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Variables in JavaScript             </H1>         </CENTER>         <SCRIPT LANGUAGE="JavaScript">  var number   temperature = 72   document.writeln("The temperature is "   +  temperature   + " degrees.")  </SCRIPT>     </BODY> </HTML> 

Note the text I'm passing to the document.writeln method this time: "The temperature is " + temperature + " degrees." . In this case, I'm using the JavaScript concatenation operator, + (which is also the addition operator for numbers), to join these three expressions into one string. The temperature variable is replaced with the value this variable contains, which is 72 , as you see in Figure 6-4.

Figure 6-4. Using variables in JavaScript.

graphics/06fig04.gif

You can also create a variable and assign a value to it at the same time with the var statement. Here's what that looks like:

 var temperature = 72  document.writeln("The temperature is " +  temperature + " degrees.") 

Besides storing numbers in JavaScript variables, you can store text strings. In this next example, I store the entire text The temperature is 72 degrees . in the variable named weatherReport , and I display the text like this:

 <HTML>      <HEAD>         <TITLE>             Using Variables in JavaScript         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Variables in JavaScript             </H1>         </CENTER>         <SCRIPT LANGUAGE="JavaScript">         <!--  var weatherReport   weatherReport = "The temperature is 72 degrees."   document.writeln(weatherReport)  //-->         </SCRIPT>     </BODY> </HTML> 

This code produces the same display as you see in Figure 6-4.

Note the name weatherReport here. The convention in JavaScript is to use lowercase names for variables and, if you create a name by joining several words, to capitalize the first letter of the second word, the third word, and so on. Names for JavaScript variables obey the same rules as the names for XML elements. Here are a few examples:

  • counter

  • numberOfLinks

  • countLeftUntilFinished

  • oneOfThoseVeryVeryVeryLongVariableNames



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net