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.
Previous: JavaScript: return statement
Next: JavaScript: Function
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/javascript/statements/const.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics