String () and. the difference between toString () Is that String () and. toString () can be converted to the String type, but String () and. there are still differences between toString () and
1. toString () can convert all data into strings, but exclude null and undefined.
For example, convert false to string type.
《script》 var str = false.toString(); console.log(str, typeof str);《script》
The returned result is false, string
Check whether null and undefined can be converted into strings.
《script》 var str = null.toString(); console.log(str, typeof str);《script》
Result program error
《script》 var str = undefined.toString(); console.log(str, typeof str);《script》
The program also reports an error
You can write a number in the. toString () brackets to represent the hexadecimal string.
Binary:. toString (2 );
Octal:. toString (8 );
Decimal:. toString (10 );
Hexadecimal:. toString (16 );
2. String () can convert null and undefined into strings, but it cannot be converted into strings.
For example, convert null to a string
《script》 var str = String(null); console.log(str, typeof str);《script》
The returned result is null, string
Convert undefined to a string
《script》 var str = String(undefined); console.log(str, typeof str);《script》
The returned result is undefined, string