各位版上的大神們好,想請問各位:
let age = 27;
age.toString();
if (age===27) {
console.log("number");
} else if (age==="27") {
console.log("string");
} else {
console.log("I don't know.");
}
這個例子中,結果會是『number』,因為 age 是 number,只有 age.toString 是 string 對嗎?
第二個例子:
let friends = ["John", "Sandy", "Alex", "Jim", "Greg"];
let friends = ["John", "Sandy", "Alex", "Jim", "Greg"];
friends.push("Harry");
console.log(friends);
這個例子中,結果會是["John", "Sandy", "Alex", "Jim", "Greg", "Harry"]
但,為什麼第一個例子中 age 使用了 .toString() 後,『age』 本身並沒有變成 string;但在第二個例子中,friends 使用了 .push("Harry") 後,『friends』本身卻改變了?
感謝各位!
作者:
laechan (揮淚斬馬雲)
2022-06-20 17:01:00你只能習慣有些就是這樣,有些就是那樣(如 array.sort() )不想記就是寫code時多花一些時間在debug我是更懶連push是啥平常都沒記,要用時google就好了像VB是Cint/Cstr,到javascript變 parseInt/toString = =若你常寫常用則自然會知道啥時要用 變數=xxx, 啥時不用