
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Const
The JavaScript const declarations create variables that cannot be reassigned to some other value or redeclared later. It was introduced in ES2015.
Following is the code for JavaScript const declaration −
Example
<!DOCTYPE html> <html> <head> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } </style> </head> <body> <h1>Const example</h1> <button class="Btn">CLICK HERE</button> <p class="sample"></p> <h3>Click the above button to try changing const value</h3> <script> let sampleEle = document.querySelector(".sample"); const a = 33; sampleEle.innerHTML = "a = " + a + "<br>"; document.querySelector(".Btn").addEventListener("click", () => { try { a = 44; } catch (err) { sampleEle.innerHTML += "Error : " + err; } }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button to change constant value −
- Related Articles
- Const vs Let in JavaScript.
- Difference between const int*, const int * const, and int const * in C
- Difference Between Static and Const in JavaScript
- Difference between const int*, const int * const, and int const * in C/C++?
- What is the difference between const int*, const int * const, and int const *?
- Difference between const char* p, char * const p, and const char * const p in C
- How to use 'const' keyword in JavaScript?
- Variable defined with a reserved word const can be changed - JavaScript?
- Const Qualifier in C
- Const cast in C++
- Const member functions in C++
- const keyword in Dart Programming
- Declare a const array in C#
- Compare define() vs const in PHP
- "static const" vs "#define" vs "enum" ?

Advertisements