In this blog, we will use the most common methods to declare variables using keywords such as var, let, and const. Each keyword has its own functionality that varies according to the requirements.
This post serves the following learning outcomes:
- Using the var keyword to declare a variable in JavaScript
- Using the let keyword to declare a variable in JavaScript
- Using the const keyword to declare a variable in JavaScript
What is the key difference between var, const and let keywords?
As mentioned earlier, the var const, and the let keywords can be used for declaring variables in JavaScript. Before starting the article, the user must familiarize the key difference between the above keywords.
Var keyword is used globally and can be retrieved anywhere in the code. It provides the redeclaration and updates features that cause bugs. To overcome the problem, let and const keywords have been introduced. The let keyword gives local access and provides an update feature, but does not give the re-declaration. The const keyword gives local access like the let keyword but does not provide the update and declaration features.
Method 1: Using the var keyword to declare variables in JavaScript
The keyword var is mostly used to declare variables that can be reassigned in JavaScript. Basically, the main purpose of the var keyword is to access the variable globally. If you declare a variable with the var keyword, it can be used globally and also provide a facility to change its value in code.
The syntax of the var keyword is written below.
Syntax
In the above syntax, the var is the keyword where the var_Name is the user-defined name for the variable. The var_Value denotes the value that will be stored in the variable named as var_Name.
Example Code:
var var_Name = "Welcome to JavaScript";
console.log(var_Name);
In the above JavaScript code, var_Name is used to declare the variable that stores a string “Welcome to JavaScript”. In the next line, var_Name is displayed using the console.log() method.
Output:
In the input part, the var_Name is declared in the 1st line of the script. After that, information that is stored in var_Name is displayed using the console.log() method.
In the output part, the “Welcome to JavaScript” message is displayed as output in the browser console.
Method 2: Using the let keyword to declare variables in JavaScript
One of the declaration methods in JavaScript is using the let keyword. It is the updated form of the var keyword. The let keyword has limited scope. The usage of this keyword is briefly discussed in this section to declare variables in JavaScript.
The syntax of the let keyword is given below.
Syntax:
In the above JavaScript syntax, the let is used as a keyword, and var_Name is the variable that stores the value of “var_Value”.
Example Code:
let var_Name = "Welcome to JavaScript";
console.log(var_Name);
In the above JavaScript code, the let keyword is used to declare the variable that stores a string “Welcome to JavaScript”. Furthermore, var_Name is displayed using the console.log() method.
Output:
The message “Welcome to JavaScript” is displayed as output in the browser console using the let keyword in JavaScript.
Method 3: Using the const keyword to declare variables in JavaScript
The keyword const is used to declare a variable but once the value is assigned, it can not be changed later in JavaScript. The let keyword has limited scope.
The syntax of the const keyword in JavaScript is given below.
Syntax:
The const is used as a keyword that stores the value “var_Value” in the var_Name variable.
Let’s use the const keyword to declare a variable.
Example Code:
const var_Name = "Welcome to JavaScript";
console.log(var_Name);
In the above JavaScript code, var_Name is used as a variable based on the const keyword. It stores a string “Welcome to JavaScript” that is displayed as output using the console.log() method.
Output:
The output displayed in the above figure shows:
- the var_Name variable is declared in the first line using the const keyword, and the string “Welcome to JavaScript” is stored in var_Name.
- At the end, the string is displayed using the console.log() method.
In this post, you learned three different methods for declaring variables in JavaScript.
Conclusion
JavaScript offers the let, const, and var keywords for declaring variables. All these keywords differ in scope. This post demonstrates all the possible methods which are utilized for declaring variables in JavaScript. Each method refers to one keyword that contains its syntax and an example. For better understanding, we have also provided differences between the var, let, and const keywords.