如何在單個語句中建立多個 JavaScript 變數?
我們可以使用`var`、`let`和`const`關鍵字在一個語句中建立多個 JavaScript 變數,這些關鍵字用於在JavaScript中宣告變數。我們可以用逗號 (,) 將多個變數連線在一起,在一個語句中宣告,而不是分別宣告每個變數。由於 JavaScript 是一種**弱型別語言**,因此可以在同一語句中宣告多種資料型別的變數。
語法
var variable1 = value1, variable2 = value2, variable3 = value3;
這裡,`var`是用於宣告變數(例如:`variable1`、`variable2`、`variable3`)併為其賦值的關鍵字。
示例 1
在下面的程式碼片段中,我們將在一個語句中宣告三個變數 a、b 和 c,並分別為它們賦值 3、9 和 5。
<!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>
但是這種宣告模式不限於相同型別的資料。我們可以以相同的方式宣告整數、浮點數、字串、陣列以及物件。
示例 2
在下面的程式碼片段中,我們將建立一個整數變數 a,一個字串變數 b 和一個數組變數 c,所有這些都在同一個語句中。
<!DOCTYPE html> <html> <head> <title>Creating multiple variables of different data types in a single statement</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>
甚至可以使用表示式來初始化布林變數,並將其與其他資料型別變數一起連線。
示例 3
在下面的程式碼片段中,我們將建立 4 個變數,它們包含不同型別的數值:整數、布林值和陣列。
<!DOCTYPE html> <html> <head> <title>Creating multiple variables using expressions in a single statement</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>
類似地,`let`和`const`等其他關鍵字也可以用於在一個語句中宣告多個區域性作用域變數。
**注意** - 但是,`const`變數一旦初始化就不能修改。
示例 4
在下面的程式碼片段中,將丟擲錯誤,因為我們試圖修改一個`const`變數。
<!DOCTYPE html> <html> <head> <title>Creating multiple objects in a single statement</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; // this line does not execute document.getElementById("result").innerHTML = "value of a is " + a + "<br/>value of b is " + b; </script> </body> </html>
使用`const`的一個例子是宣告物件。也可以在同一個語句中宣告多個物件。
示例 5
在下面的程式碼片段中,我們建立了兩個物件 person1 和 person2,它們具有 name 和 age 屬性。
**注意**,但是每個物件的元素不必相同。
<!DOCTYPE html> <html> <head> <title>Creating multiple objects in a single statement</title> </head> <body> <div id = "result"> </div> <script> const person1 = {name : "jhon",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>
結論
兩種宣告方式都有其自身的用途。分別宣告變數使我們可以靈活地透過更改用於宣告變數的關鍵字來快速更改變數的作用域,而在同一語句中宣告多個變數可以減少冗餘程式碼。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP