w3resource
JavaScript Statements

JavaScript: const Statement

Description

A constant is an identifier for a simple value. The value cannot be modified during the script's execution. In JavaScript, const statement creates a constant. Constants follow the same scope rules as JavaScript variables.

Version

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5.

Syntax

const varname1 = value1 , varname2 = value2,... varnameN = valueN

Parameters

varname1, varname2......varnameN : Constant names.

value1, value2......value3 : Value of the constant.

Example:

The following web document displays the height and width and area of the rectangle.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript const statement :  Example-1</title>
<link rel="stylesheet" type="text/css" href="example.css">
</head>
<body>
<h1>JavaScript : const statement</h1>
<script src="const-statement-example1.js"></script>
</body>
</html>

JS Code

const height = 400;
const width = 200;
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Height is : "+height+" Width
is : "+ width +". So, Area of the Rectangle is " + height*width +
" sq.ft.");
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);

View the example in the browser

Browser compatibility

- Supported Firefox & Chrome (V8).
- In the case of Safari 5.1.7 and Opera 12.00, you can change the value of the const after defining a variable with const.
- It is not supported in Internet Explorer 6-9, or in the preview of Internet Explorer 10.



New Content: Composer: Dependency manager for PHP, R Programming