Visit Mozilla.org

Core JavaScript 1.5 Reference:Statements:const

From MDC


Contents

[edit] Summary

Declares a read-only, named constant.

Statement
Implemented in: JavaScript 1.5, NES 6.0 (Netscape extension, C engine only)

[edit] Syntax

const varname1 [= value1], varname2 [= value2], ..., varnameN [= valueN];

[edit] Parameters

varnameN 
Constant name. It can be any legal identifier.
valueN 
Value of the constant. It can be any legal expression.

[edit] Description

Creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.

The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.

A constant cannot share its name with a function or a variable in the same scope.

const is a Mozilla-specific extension, it is not supported by IE, but has been supported by Opera since version 9.0.

[edit] Examples

[edit] Example: Using const

The following example produces the output "a is 7."

const a = 7;
document.writeln("a is " + a + ".");

[edit] See also