Signup/Sign In
PUBLISHED ON: FEBRUARY 23, 2023

JavaScript Program To Work With Constants

This tutorial will teach how to make a JavaScript Program To Work With Constants. To understand this article we need to have a decent knowledge of JavaScript Variables and Constants. It's an easy job that can be done with a few lines of code. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

Work With Constants

JavaScript ES6 has introduced the const keyword to work with constants. const denotes that the reference to value is constant and cannot be changed.

Block Level Scope: This scope restricts the variable that is declared inside a specific block, from access by the outside of the block. The let & const keyword facilitates the variables to be block scoped.

The arr array value is changed and a new element is added. In an array, the values can be changed.

Program To Work With Constants

// JavaScript Program to include constants

const a = 15;
console.log(a);

// constants are block-scoped in Javascript
{
    const a = 150;
    console.log(a);
}
console.log(a);

const arr = ['work', 'exercise', 'eat', 'sleep', 'drink'];
console.log(arr);

// add up elements to arr array
arr[5] = 'JavaScript is Love!';
console.log(arr);


15
150
15
[ 'work', 'exercise', 'eat', 'sleep', 'drink' ]
[ 'work', 'exercise', 'eat', 'sleep', 'drink', ' JavaScript is Love!' ]

Conclusion

We have developed a dynamic, constant-using JavaScript application. The goal of this tool is to give programmers a better understanding of JavaScript constants and how they can be used to write more maintainable and efficient code.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.