2017-10-09 20 views
-4

私はこれら2つのコードがどのように異なっているかを理解しようとしています。コメント欄でjavascript;以下の2つのコードの違いは何ですか

var bill=10.25+3.99+7.15; 
var tip = bill*0.15; 
var total=bill+tip; 
total = total.toFixed(2); 
console.log("$"+total); 

そして

var bill=10.25+3.99+7.15; 
var tip = bill*0.15; 
var total=bill+tip; 
console.log("$"+total.toFixed(2)); 
+1

彼らは実質的な意味ではありません。 – glennsl

+2

それらのうちの1人は、出力する前に 'total'変数を更新します。もう片方はしません。 – David

+0

どうしてあなたは違うと思いますか? 'console.log(" $ "+((10.25 + 3.99 + 7.15)* 1.15).toFixed(2));'と同じです。 – Xufox

答えて

1

説明:

<script> 
    var bill=10.25+3.99+7.15; 
    var tip = bill*0.15; 
    var total=bill+tip; // total is number 
    total = total.toFixed(2); // total has been converted into a string with only two decimal places 
    console.log("$"+total); //prints out the '$' along with value of total variable which is a 'string' 
    typeof total; //returns "string" 
    </script> 

<script> 
     var bill=10.25+3.99+7.15; 
     var tip = bill*0.15; 
     var total=bill+tip; //total is number 
     console.log("$"+total.toFixed(2)); //even after this statement the type of 'total' is integer, as no changes were registered to 'total' variable. 
     typeof total; //returns "number" 
    </script> 
関連する問題