
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create constants in JavaScript? Explain with examples.
Following is the code to create constants in JavaScript −
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
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button to change constant value −
- Related Questions & Answers
- What is JavaScript closure? Explain with examples.
- Explain JavaScript Regular Expression modifiers with examples
- Explain the finally Statement in JavaScript with examples.
- Explain try and catch statements in JavaScript with examples.
- Explain the Error Name Values in JavaScript with examples.
- Explain Python Matrix with examples
- Explain Stack in Python with examples
- Explain 5NF with examples in DBMS
- How to verify if a JavaScript object is an array? Explain with examples.
- How to create Variables and Constants in C++?
- How to declare string constants in JavaScript?
- How to define integer constants in JavaScript?
- How to create a JSON object in JavaScript? Explain with an example.
- What is Queue in Python? Explain with examples
- What is 1NF in DBMS? Explain with examples
Advertisements