1,. toString ()All of the data can be converted to a string, butexclude null and undefined
For example, to convert false to string type
<script>
var
str =
false
.toString();
console.log(str,
typeof
str);
</script>
The returned result is false,string
See if null and undefined can be converted to string JavaScript
<blockquote style=
"margin-right: 0px;"
dir=
"ltr"
><pre
class
=
"html"
name=
"code"
><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>
Program also error
. toString () parentheses can write a number that represents the binary, corresponding to the binary string
Binary:. toString (2);
Octal:. toString (8);
Decimal:. toString (10);
Hex:. toString (16);
Such as:
var c = 123; Console.log (C.tostring (8)); result 173
2, String ()You can convert null and undefined to a string.
For example, converting 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
Console.log (String (077)); Returns the result: 63 (if starting with 0 or starting with 0x will be first converted to a binary number, in a string)
JS in string () and ToString ()