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
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> |