Home > Article > Web Front-end > What is the javascript string conversion function?
javascript string conversion function: 1. toString() function, which can convert all data into strings, the syntax is "number.toString(radix)"; 2. String() function, which can convert objects The value is converted to a string with the syntax "String(js object)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript string conversion function - String() and toString()
1, .toString()
All data can be converted into strings, but null
and undefined
must be excluded. For example, 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 into strings javascript
<blockquote style="margin-right: 0px;" dir="ltr"> <pre class="html" name="code"> <script> var str = null.toString(); console.log(str, typeof str); </script>
As a result, the program reports an error
<script> var str = undefined.toString(); console.log(str,typeof str); </script>
Program Also reported an error
.toString()
You can write a number in the brackets, representing the base, corresponding to the base string
Binary: .toString(2);
Octal: .toString(8);
Decimal: .toString(10);
Hexadecimal: .toString(16);
For example:
var c = 123 ; console.log(c.toString(8));
The result is
173
2, String()
can convert null and undefined 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>
Return The result is undefined, string
console.log(String(077));
returns the result: 63 (if it starts with 0 or 0x, it will be converted to a base number first, and then converted to a string)
[Related Recommended: javascript learning tutorial】
The above is the detailed content of What is the javascript string conversion function?. For more information, please follow other related articles on the PHP Chinese website!