Introduction
The keywords var, let, and const are used in the JavaScript programming language. JavaScript is widely used in building web applications, mobile applications, Game development, API Development, etc. This article will give you a description of the var, let, and const keywords used in the JavaScript programming. These keywords are used for assigning a value to a variable, but all three keywords are used in different scenarios. And, they perform a unique function in JavaScript programming.
var keyword in JavaScript programming
The var is used for assigning a value to a variable in JavaScript. We use var when we need to be defined globally. It means the value should not be limited to a certain block of code but can be accessed outside the block as well, if the value is declared outside the function.
Given below are the different features of using the var keyword in JavaScript
1. Variable Declaration: The primary function of the var keyword is to assign a value to a variable.
2. Redeclaration: The value assigned to the var keyword can be updated, as per the programmer's choice and requirement.
3. Scope: The scope of this keyword is within the function as well as outside the function, depending on how the value is declared in a program.
Given below is the example of using var keyword in JavaScript
Example
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript var keyword</h1>
<p> Addition operation will be performed with the help of three variables. </p>
<p id = "demonstrate"></p>
<script>
var a = 15;
var b = 20;
var c = a + b;
document.getElementById("demonstrate").innerHTML = "The value of addition store in c: " + c;
</script>
</body>
</html>
Output
JavaScript var keyword
Addition operation will be performed with the help of three variables.
The value of addition store in c: 35
let keyword in JavaScript
The purpose of this keyword is to assign a value to a variable. Values declared to a variable with the help of this keyword are accessible to a block of code only where it has been declared, and they are not globally accessible.
The limit of value declared using the let keyword is within the scope of a function only where it is declared. Given below are the different features of using the let keyword in JavaScript
1. Variable Declaration: The let keyword is used for declaring a value to a variable. The let keyword is used while declaring a value to a variable.
2. Redeclaration: You can assign a different value to the same variable once it is declared. Hence, redeclaration is also allowed in the let keyword. Different data types can be stored with the help of the let keyword, and modification is also allowed for updating the value.
3. Scope: The scope of this keyword is within the range of the function where the variable has been declared. These values can not be accessed outside the function, unlike the var keyword.
Given below is the example of using let keyword in JavaScript
Example
// JavaScript program to demonstrate the working of let keyword
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript let keyword</h1>
<p> Addition operation will be performed with the help of three variables. </p>
<p id = "demonstrate"></p>
<script>
let a = 15;
let b = 20;
let sum = a + b;
document.getElementById("demonstrate").innerHTML = "The result of addition store in variable sum: " + sum;
</script>
</body>
</html>
Output:
JavaScript let keyword
Addition operation will be performed with the help of three variables.
The result of addition store in variable sum: 35
const keyword in JavaScript
The keyword const is used for declaring a constant value in JavaScript. It means once the value is declared, it cannot be modified or changed. The value will remain fixed throughout the lifetime under the const keyword. Given below are the different features of the const keyword in JavaScript.
1. Variable Declaration: The keyword const is used to declare a value for a variable. For example, const a = 20; In this example, the variable stores a constant value 20, which can not be changed or updated.
2. Redeclaration: Unlike other keywords like var and let, Redeclaration is not allowed in the const keyword. The value will remain fixed throughout the lifetime under the const keyword. This is the unique feature of the const keyword for declaring a constant value in JavaScript programming.
3. Scope: Like the let keyword, the scope of the const keyword is within the range of the function where it has been declared, and it can not be accessed globally. Thus, the visibility of the const keyword is visible under the block of code where they have been declared.
Given below is the example of using const keyword in JavaScript
Example
// JavaScript program to demonstrate the working of const keyword
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript const keyword</h1>
<p> In this program a data is stored in msg variable which can not be change. </p>
<p id = "demonstrate"></p>
<script>
const msg = "This value can not be change"
document.getElementById("demonstrate").innerHTML = "The string value are store in the msg variable which: " + msg;
</script>
</body>
</html>
Output:
JavaScript const keyword
In this program a data is stored in msg variable which can not be change.
The string value are store in the msg variable which: This value can not be change
Benefits of Learning JavaScript
In modern times, learning JavaScript (JS) is one of the best investments for anyone. Whether your aim is web development, app building, or even AI/IoT projects. JavaScript opens a large number of opportunities in order to make a carrer.
• JavaScript is a language that can be run in all browsers without extra setup.
• It is very essential for web developoment that helps to make websites interactive.
• It can also be used with Node.js for both frontend & backend.
• It offers frameworks such as React, Angular, Vue, and libraries for almost anything.
• It is beginner friendly language, which provides easy syntax. Also, it does not needed any compiler and can test directly in browser console.
• It is very high in demand for web & app developers in tech today.
• In the field of IoT & AI, its use is in hardware programming (Johnny-Five) and machine learning (TensorFlow.js).
• Also, it is used in game development and offers libraries like Phaser.js and Three.js for 2D/3D games.
• In terms of future-proof skill, JavaScript will remain relevant as long as the web exists.
Conclusion
This article describes the let, var, and const keywords used in JavaScript. These keywords play a major role in JavaScript programming. I hope this article has provided you with valuable information. If you are looking for more of this kind of information, I suggest you visit the Tpoint Tech Website, where you can find various articles on programming, along with an online compiler where you can run and test your code.
