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