有人可以為我解釋一下 JavaScript 中變數之前的加號是什麼嗎?


變數之前的加號 (+) 定義了你要使用的變數為數字變數。

在以下程式碼中,對加號進行了簡要說明。程式碼如下 −

示例

var firstValue="1000";
console.log("The data type of firstValue ="+typeof firstValues);
var secondValue=1000;
console.log("The data type of secondValue ="+typeof secondValue);
console.log("The data type of firstValue when + sign is used ="+typeof
+firstValue);
var output=+firstValue + secondValue;
console.log("Addition is="+output);

要執行上述程式,需要使用以下命令 −

node fileName.js.

輸出

此處,我的檔名是 demo149.js。這將產生以下輸出 −

PS C:\Users\Amit\JavaScript-code> node demo149.js
The data type of firstValue =string
The data type of secondValue =number
The data type of firstValue when + sign is used =number
Addition is=2000

更新日期:2020 年 9 月 11 日

3K+ 瀏覽量

開啟你的事業

完成課程並獲取認證

開始
廣告