Constants


As you can tell from the name "variable," the value in variables can change. However, you might not want the value in a variable to changefor example, you might want to store the value of pi, 3.1415926, for easy access throughout your script. Pi's not going to change; if you store it in a variable, however, you run the risk that its value might be inadvertently changed somewhere in your script. The Netscape Navigator version 6 enables you to declare constants instead, using the const statement, which you see in Table 2.2. Once declared, a constant can't change its value.

Table 2.2. The const Statement

Statement

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

const

     

x

           

Here's the syntax for this statement, much like the syntax for the var statement:

 const  constname  [=  value  ] [...,  constname  [=  value  ] ] 

Constants are just like variables except that once initialized , they can't change their values. Here's an example where I'm using a constant named pi to hold the value of pi:

(Listing 02-02.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With Arrays          </TITLE>      </HEAD>      <BODY>          <H1>Working With Arrays</H1>          <FORM NAME="Form1">          </FORM>          <SCRIPT LANGUAGE="JavaScript">          <!--  const pi = 3.1415926   document.write("Pi is " + pi + ".")  // -->          </SCRIPT>      </BODY>  </HTML> 


Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

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