JavaScript Declare Multiple Variables
In this article, we will see how to declare multiple variables in javascript. We can declare a javascript variable using ‘var’, ‘let’, and ‘const’ keywords.
- If we use the ‘var‘ keyword, it is used to declare variables in the global scope or function scope, depending on where it’s declared.
- If we use the ‘let‘ or ‘const‘ keyword to declare variables, it is only accessible within the block of code in which they are declared, including any nested blocks. We can reassigned the variables if we use ‘let’. We cannot reassign the variables using ‘const’ because it is constant, so we cannot be modified after they are declared. Click this link to know the difference between the let, var and const keywords.
What is variable declaration?
We use one of the keywords var, let, or const to declare a variable. In the below example, we declared a variable without initialization.
Ex:
var person1;
let person2;
const person3 = 5;
- In this code, we declared the variable named ‘person1‘ and initialize it to undefined using the ‘var‘ keyword.
- Then we declared the variable named ‘person2‘ and initializes it to undefined using the ‘let‘ keyword. We declared the variable with ‘let’ are block-scoped and can be reassigned later
- And we declared the variable ‘person3‘ and initialized it to the value ‘5’. We should initialize the variable when using the ‘const‘ keyword because we cannot be reassigned later.
So, If we give a name to a variable, it is called a variable declaration. We can initialize the value to that variable later, so Initialization is setting the value to the variable.
We can also declare a variable with initialize value. Let’s see the example:
Ex:
var x=10;
let y = 20;
const z = 5;
Here, we declare a variable ‘x’ and initialize it with the value ’10’ using the ‘var’ keyword. Then we declared a variable ‘y’ and initialize it with the value ’20’ using the ‘let’ keyword. Finally, we declared a variable ‘z’ and initialize it with the value ‘5’ using the ‘const’ keyword.
Why should we Declare Multiple Variables?
- Simplicity: Instead of writing multiple lines of code to declare variables individually, we can declare multiple variables at once, which can simplify your code.
- Improved readability: If we Declare multiple variables on the same line, it can make your code more readable and concise, especially when the variables are related.
- Better organization: It can help you keep your code organized, particularly if the variables are related to one another.
How to declare multiple variables?
Declare without initializing
We can declare multiple variables using ‘var’ and ‘let’ keywords and initialize them later. But we cannot do them using the ‘const’ keyword because we need to initialize the constant variable. For example:
//declare multiple variables
var var1, var2, var3;
let let1, let2, let3
//Initialzing them
var1=10;
var2=20;
var3=30;
let1=40;
let2=50;
let3=60;
//printing them
document.write("Var1 ="+var1)
document.write("Var2 ="+var2 )
document.write("Var3 ="+var3 + '</br>')
document.write("Let1 ="+let1)
document.write("Let2 ="+let2)
document.write("Let3 ="+let3)
Output:
- In the above code, we declare three variables var1, var2, and var3 using the ‘var’ keyword separated by a comma operator. Then we declare another three variables let1, let2, and let3 using the ‘let’ keyword separated by a comma.
- Then we initialize the 6 variables. If we do not initialize the value to the variable, then by default value ‘undefined’ is assigned to them explicitly.
- Finally, we print them using a document.write() method. See the output the values are correctly displayed.
Declare with initializing
We can also declare variables with initialization. In the example below, the variables are declared in a single statement, each with its own value.
var keyword:
var var1 = 10, var2 = 20 , var3 =30;
In the above example, we declare three variables using the ‘var‘ keyword var1, var2, and var3, and initialize them with the values 10, 20, and 30, respectively.
let keyword:
let let1=10, let2=20, let3=30;
In the above example, we declare three variables using the ‘let‘ keywords let1, let2, and let3, and initialize them with the values 10, 20, and 30, respectively.
const keyword:
const cons1 = 10, cons2 = 20, cons3 = 30;
In the above example, we declare three variables using the ‘const‘ keywords cons1, cons2, and cons3, and initialize them with the values 10, 20, and 30, respectively.
Using destructuring assignment:
const [x, y, z] = [10, 20, 30];
In this above example code, we use destructuring assignment to declare three variables x, y, and z, and initialize them with the values 10, 20, and 30. So, the first element in the array, ’10’, is assigned to the variable ‘x’. The second element in the array, ’20’, is assigned to the variable ‘y’. And the third element in the array, ’30’, is assigned to the variable ‘z’. We can log the variable using the console.log() method like:
console.log(x); // output 10
console.log(y); // output 20
console.log(z); // output 30
Destructuring assignments can be useful when working with arrays or objects. And this method allows you to extract and assign values to variables in a concise and readable way.
Conclusion
You can declare multiple variables in javascript and initialize the value whenever you need or initialize it with a declaration. When we declare multiple variables we must use the comma operator correctly.