Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.



JavaScript: Tips of the Day

How to insert an item into an array at a specific index (JavaScript)?

What you want is the splice function on the native array object.

arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:

var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());

Ref: https://bit.ly/2BXbp04