*
」乘法運算子 (P2-34)let result = 5 * 2;
console.log(result); // 10
如果其一是字串,JavaScript會嘗試將字串轉換為數字後進行計算。
let result = "5" * "2";
console.log(result); // 20
如果字串不能轉換為有效的數字,結果是NaN
(Not-a-Number),因為JavaScript中沒有直接用乘法運算符來重複字串。
let result = "17" * "day";
console.log(result); // NaN
若要重複字串,可以使用String.prototype.repeat方法。
let mood = "happy!";
let count = 3;
let feel= mood.repeat(count);
console.log(feel); // "happy!happy!happy!"
/*複習一下,串連多個字串 (P2-7)*/
let feel = "I fell "
let mood = "happy!";
let count = 3;
let result= feel + mood.repeat(count);
console.log(result); // "I feel Happy!Happy!Happy!"
/*複習一下,使用字串模板 `${}`*/
let mood = 'Happy!';
let feel = `I feel ${mood.repeat(3)}`;
console.log(feel); // "I feel Happy!Happy!Happy!"
布林值true
會被轉換為1,false
會被轉換為0。在進行乘法運算時會影響結果。
let trueValue = true;
let falseValue = false;
let resultTrue = trueValue * 10; // true轉換為1,結果是10
console.log(resultTrue); // 10
let resultFalse = falseValue * 10; // false轉換為0,結果是0
console.log(resultFalse); // 0
let result = Math.pow(10, 1000);
console.log(result); //Infinity