Wednesday, February 9, 2011

Constants

To store a piece of data that can never change, you need a constant.
Constants are created just like initialized variables, but you use the const keyword instead of var.
And the "initial" value becomes the permanent value.

Example:

const + constant name + = + constant value + ;

The syntax is the same but constants are usually named in all capital letters to make them STANDOUT in your code.

These are handy for storing information that you might directly code into a script, like a sales tax rate. Instead of using a number, your code is easier to understand if you use a constant with a descriptive name like TAXRATE.
And if you need to make changes to the value, you can do it in one place...where the constant is defined instead of locating it and having to change it throughout the document.

No comments:

Post a Comment