JavaScript Constant (const) Tutorial

JavaScript Constant (const) Tutorial

Like other programming languages like C,PHP,Java … JavaScript also has Constant feature. As per developer.mozilla.org “Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can’t be redeclared “.

ES2015, the latest version of JavaScript, has a notion of const. So, most of the modern browser(latest version) like firefox, chrome, edge has const support.

JavaScript Constant Syntex :

const constantName = value1 , varname2 = value2,… varnameN = valueN;

JavaScript Constant Example

Create file called constant.html with code below

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>JavaScript Constant example</title>
</head>
<body>
<h1>JavaScript Constant example</h1>
<script>
const height = 400;
const width = 400;
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);
</script>
</body>
</html>

Now run web page constant.html in latest version of Chrome, Firefox, EDGE ,IE10.

Leave a Reply

Your email address will not be published. Required fields are marked *

Do you have any Query / Suggestion