How to create many Javascript variables in a single statement?

We can create many JavaScript variables in a single statement using the var, let, and const keywords. Instead of declaring every variable individually, we can chain many variables together with commas (,) in a single statement. Since JavaScript is a loosely typed language, variables of different data types can be declared in the same sentence as well.

Syntax

var variable1 = value1, variable2 = value2, variable3 = value3;
let variable1 = value1, variable2 = value2, variable3 = value3;
const variable1 = value1, variable2 = value2, variable3 = value3;

Using var Keyword

The var keyword can declare multiple variables with different data types in a single statement.

<!DOCTYPE html>
<html>
<head>
   <title>Creating multiple variables in a single statement</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var a = 3, b = 9, c = 5;
      document.getElementById("result").innerHTML = "value of a is :" + a + "<br/>value of b is :" + b + "<br/>value of c is :" + c;
   </script>
</body>
</html>
value of a is :3
value of b is :9
value of c is :5

Multiple Data Types in Single Statement

JavaScript allows declaring variables of different data types (integers, strings, arrays) in the same statement.

<!DOCTYPE html>
<html>
<head>
   <title>Creating multiple variables of different data types</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var a = 10, b = "name", c = [1,2,3];
      document.getElementById("result").innerHTML = "value of a is :" + a + "<br/>value of b is :" + b + "<br/>value of c is :" + c;
   </script>
</body>
</html>
value of a is :10
value of b is :name
value of c is :1,2,3

Using Expressions in Variable Declaration

Variables can be initialized with expressions and boolean values alongside other data types.

<!DOCTYPE html>
<html>
<head>
   <title>Creating multiple variables using expressions</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var a = 10, b = (10 > 5), c = [1,2,3], d = ("five" < "animal");
      document.getElementById("result").innerHTML = "value of a is :" + a + "<br/>value of b is :" + b + "<br/>value of c is :" + c + "<br/>value of d is :" + d;
   </script>
</body>
</html>
value of a is :10
value of b is :true
value of c is :1,2,3
value of d is :false

Using const Keyword

The const keyword can also declare multiple variables, but they cannot be modified once initialized.

<!DOCTYPE html>
<html>
<head>
   <title>Creating multiple const variables</title>
</head>
<body>
   <div id="result"></div>
   <script>
      const a = 5, b = 9;
      document.getElementById("result").innerHTML = "value of a is " + a + "<br/>value of b is " + b;
      
      // The below line will generate an error
      // a = 10;
   </script>
</body>
</html>
value of a is 5
value of b is 9

Creating Multiple Objects

Multiple objects can be declared in a single statement. Each object can have different properties.

<!DOCTYPE html>
<html>
<head>
   <title>Creating multiple objects in a single statement</title>
</head>
<body>
   <div id="result"></div>
   <script>
      const person1 = {name: "John", age: 15}, person2 = {name: "Doe", age: 21};
      var output = document.getElementById("result");
      output.innerHTML += "First person has name " + person1.name + "<br/>First person has age " + person1.age + "<br/>";
      output.innerHTML += "<br/>Second person has name " + person2.name + "<br/>Second person has age " + person2.age;
   </script>
</body>
</html>
First person has name John
First person has age 15

Second person has name Doe
Second person has age 21

Key Points

  • Use commas to separate multiple variable declarations
  • Different data types can be mixed in the same statement
  • const variables cannot be reassigned after initialization
  • let and const have block scope, while var has function scope

Conclusion

Declaring multiple variables in a single statement reduces code redundancy and improves readability. Choose the appropriate keyword (var, let, or const) based on your scoping and mutability requirements.

Updated on: 2026-03-15T23:18:59+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements