Detailed explanation of the difference between toString of and String of in js
- 2021-08-05 08:58:29
- OfStack
We know that String () and. toString () are convertible to string types, but there is a difference between String () and. toString ()
1. toString () converts all data to strings, but excludes null and undefined
For example, converting false to string type
var str = false.toString();
console.log(str, typeof str);
The returned results are false, string
See if null and undefined can be converted to the string javascript
var str = null.toString();
console.log(str, typeof str);
Results the program reported an error
var str = undefined.toString();
console.log(str, typeof str);
The program also reports errors
. toString () in parentheses can write 1 number, representing the binary, corresponding to the binary string
Binary:. toString (2);
Octal:. toString (8);
Decimal:. toString (10);
106-ary:. toString (16);
2. The web page code String () can convert null and undefined into strings, but it cannot be converted into binary strings
For example, converting null to a string
var str = String(null);
console.log(str, typeof str);
The returned results are null, string
Convert undefined to String
var str = String(undefined);
console.log(str, typeof str);
The returned results are undefined, string