Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

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.

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments