| by Arround The Web | No comments

How to declare variables in different ways in JavaScript?

JavaScript is a popular scripting language that is used worldwide. In JavaScript, a variable is used to store the value of data that can be modified in the future. There are different ways that are adapted to declare variables in JavaScript.

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:

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

var var_Name = "var_Value;

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:

// declare variable using var keyword
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:

let var_Name = "var_Value";

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:

// declare variable using let keyword
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:

const var_Name = "var_Value";

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:

// declare variable using const keyword
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.

Share Button

Source: linuxhint.com

Leave a Reply