Home > Web Front-end > JS Tutorial > What is the javascript string conversion function?

What is the javascript string conversion function?

青灯夜游
Release: 2023-01-06 11:17:39
Original
3534 people have browsed it

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)".

What is the javascript string conversion function?

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>
Copy after login

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>
Copy after login

As a result, the program reports an error

<script>
  var str = undefined.toString();
  console.log(str,typeof str);
</script>
Copy after login

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));
Copy after login

The result is

173
Copy after login

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
Copy after login

The returned result is null, string

Convert undefined to a string

<script>
  var str = String(undefined);
  console.log(str, typeof str);
</script>
Copy after login

Return The result is undefined, string

console.log(String(077));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template