How to declare multiple Variables in JavaScript?

How to declare multiple Variables in JavaScript?

At 6/7/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. There are different methods to declare multiple variables, these are:

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:

Javascript

let x = 20;
let y = 'F';
let z = "Freesad";
  
console.log("x: ", x);
console.log("y: ", y);
console.log("z: ", z);

Output
x:  20
y:  F
z:  Freesad

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:

Javascript

let x = 20,
    y = 'F',
    z = "Freesad";
  
console.log("x: ", x, "\ny: ", y, "\nz: ", z);

Output

x:  20 
y:  F
z:  Freesad

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:

Javascript

const [x, y, z] = [20, 'F', "Freesad"];
  
console.log("x: ", x, "\ny: ", y, "\nz: ", z);

Output
x:  20 
y:  F
z:  Freesad

حقوق التأليف والنشر

نحن نحترم حقوق الملكية للآخرين ونحرص دائمًا على عدم التعدي على حقوقهم، لذا يحق للمؤلفين ودور النشر المطالبة بإزالة رابط تحميل مقال أو كتاب من الموقع. إذا وجدت مقالاً أو كتاباً خاصاً بك ولم توافق على نشر رابط التحميل أو كان لديك اقتراح أو شكوى راسلنا عبر تواصل معنا, أو عن طريق البريد الإلكتروني في: support@freewsad.com.

المزيد عنا