String()和toString()的區別和應用



應用
String()和toString()都是將其他類型的變量轉換為字符串類型。

let a =1;
let b = 123;
String(a); // '1'
typeOf(a); // String
typeOf(b); //Number
b.toString(); // '123'
typeOf(b); //String;


區別
toString()無法轉換null和undefined

let a;
let b=null;
a.toString();//Uncaught TypeError: Cannot read property 'toString' of undefined
b.toString(); //Uncaught TypeError: Cannot read property 'toString' of null
String(a); //"undefined"
String(b);//"null"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM