Section 2.4. Constants: Named but Not Variables


2.4. Constants: Named but Not Variables

There are times when you'll want to define a value once, and then have it treated as a read-only value from that time forward. The keyword const is used to create a JavaScript const:

const CURRENT_MONTH = 3.5;

The constant can be of any value, and since it can't be assigned or reassigned a value at a later time, it's initialized to its constant value when defined.

Just as with variables, a JavaScript constant has global and local scope. I use constants at a global level, primarily because they contain a value I want to be accessible (and unchanged) by a JavaScript block.




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

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