How to declare multiple Variables in JavaScript?
Last Updated :
07 Dec, 2023
In this article, we will see how to declare multiple Variables in JavaScript. The variables can be declared using var, let, and const keywords. Variables are containers that store some value and they can be of any type.
These are the following ways to declare multiple variables:
Declaring Variables Individually
In this case, we will declare each variable using the var, let, or const keywords.
Syntax:
let x = 20;
let y = 30;
let z = 40;
Example: In this example, we are declaring three different variables.
Javascript
let x = 20;
let y = 'G' ;
let z = "GeeksforGeeks" ;
console.log( "x: " , x);
console.log( "y: " , y);
console.log( "z: " , z);
|
Output
x: 20
y: G
z: GeeksforGeeks
Declaring Variables in a Single Line
You can declare multiple variables in a single line using the var, let, or const keyword followed by a comma-separated list of variable names.
Syntax:
let x = 20, y = 30, z = 40;
Example: In this example, we are defining the three variables at once.
Javascript
let x = 20,
y = 'G' ,
z = "GeeksforGeeks" ;
console.log( "x: " , x, "\ny: " , y, "\nz: " , z);
|
Output
x: 20
y: G
z: GeeksforGeeks
Using Destructuring Assignment
You can also use de-structuring assignments to declare multiple variables in one line and assign values to them.
Syntax:
const [var1, var2, var3] = [val1, val2, val3];
Example: In this example, we are declaring three different variable by destructuring them at once.
Javascript
const [x, y, z] = [20, 'G' , "GeeksforGeeks" ];
console.log( "x: " , x, "\ny: " , y, "\nz: " , z);
|
Output
x: 20
y: G
z: GeeksforGeeks
Share your thoughts in the comments
Please Login to comment...