Pure Programmer

# Constants

Often when we write a program, we need to use the same literal value over and over. Not only is it tedious to key in the same literal over and over, but it is also prone to error. For example if you write a program that uses trigonometry functions, we would often need the value for [[π]] which is approximately 3.141592653589793. You can see how tedious it would be to type this literal value not to mention the likelihood of transposition or other errors. Having a simple named constant symbol such as `PI` would make writing such a program much easier.

It is also possible that we need to use literal values in our program that change from time-to-time but not during the running of our program. Values like local sales tax rate or the [[cosmological constant]] only change infrequently so having a single place to make the required change when it is necessary helps greatly with program maintenance.

## Types

JavaScript constants and variables are not declared with a specific data type in mind. Instead they are generally declared as existing then we can put a value of any type we wish into them. This is idea is known as weak typing. It makes it easy for the programmer to create constants and variables but can lead to some types of run-time errors because the language interpreter can not check to see if the proper type of value has been used. The JavaScript Number type is double-precision 64-bit binary format IEEE 754 value. This means that it can represent integer values between -9,007,199,254,740,991 and 9,007,199,254,740,991. For floating point values the number of significant digits is about 16 and the exponent can range from -308 to 308.

## Declaration Syntax

Named constants must be initialized to a specific value when they are declared. Once initialized, they can not have their value changed during the execution of the program. The syntax for a constant declaration consists of the keyword `const`, the name of the constant (see rules for identifiers), the equal sign (`=`), the initializer value for the constant and ends with a semicolon (`;`). Examples of statements used to declare a named constants are illustrated below.

### Questions

1. What are some examples of integer constants?
2. What are some examples of floating point constants?
3. What are some examples of string constants?

### Projects

More ★'s indicate higher difficulty level.

## References

• [[JavaScript Language Reference]], Mozilla Developer Network
• [[Mozilla Developer Network]]