How to Declare Multiple Variables in JavaScript?
Last Updated :
25 Oct, 2024
JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.
Declaring Multiple Variables Individually
The most simple way to declare variables is to declare them one by one using var, let, or const keyword. You can directly assign values and can access them using the variable name.
Syntax
let x = 20;
let y = 30;
let z = 40;
Example: Declaring three different variables using let keyword.
JavaScript
let x = 20;
let y = 'G';
let z = "GeeksforGeeks";
console.log("x: ", x);
console.log("y: ", y);
console.log("z: ", z);
Outputx: 20
y: G
z: GeeksforGeeks
Declaring Multiple Variables in a Single Line
You can declare multiple variables in one line using a comma-separated list with var, let, or const keyword. This approach can make the code more concise.
Syntax
let x = 20, y = 30, z = 40;
Example: Declaring and assigning three different variables at once.
JavaScript
let x = 20, y = 'G',
z = "GeeksforGeeks";
console.log("x: ", x);
console.log("y: ", y);
console.log("z: ", z);
Outputx: 20
y: G
z: GeeksforGeeks
Using Destructuring Assignment
Destructuring assignment is a modern feature in JavaScript that allows you to unpack values from arrays or properties from objects into distinct variables in a single line. This is especially useful when dealing with arrays or objects.
Syntax
const [var1, var2, var3] = [val1, val2, val3];
Example: Declaring three different variables by destructuring them at once.
JavaScript
const [x, y, z] = [20, 'G', "GeeksforGeeks"];
console.log("x: ", x);
console.log("y: ", y);
console.log("z: ", z);
Outputx: 20
y: G
z: GeeksforGeeks
Similar Reads
How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab
2 min read
How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
2 min read
How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari
3 min read
How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var
2 min read
How to declare namespace in JavaScript ? The coding standard of assigning scope to identifiers (names of types, functions, variables, and so on) to avoid conflicts between them is known as a namespace. It is a framework for a collection of identifiers, functions, and methods. It gives its contents a sense of direction so that they are easi
3 min read