Home > Web Front-end > JS Tutorial > body text

Detailed graphic explanation of two methods of converting arrays to strings in JavaScript

yulia
Release: 2018-10-09 14:22:35
Original
20469 people have browsed it

JavaScript is often used in front-end development, so do you know how to convert a JS array into a string? This article will tell you about the method of converting JS array objects to strings. Friends who are interested can refer to it. I hope it can help you.

1. Join converts an array into a string

The join() method can convert all elements in the array into a string, and all major browsers Support join() method.

Example: Define an array, click the button to convert the array into a string, and display the type and result on the page.

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <p id="demo">点击按钮将数组转为字符串</p>
  <button onclick="myFunction()">点击</button>
 </body>
 <script type="text/javascript">
  function myFunction(){
   var animal = ["dog", "cat", "elephant", "tiger"];
   var str=animal.join(&#39;&#39;);
   document.write("类型是:"+typeof(str)+",字符串是:"+str);   
  }
 </script>
</html>
Copy after login

Rendering:

Detailed graphic explanation of two methods of converting arrays to strings in JavaScript

The join() method can set different delimiters to separate the content in the string. For example, join(‘/’) can achieve the following effects.

Rendering:

Detailed graphic explanation of two methods of converting arrays to strings in JavaScript

2. toString converts the array into a string

toString() method also Array objects can be converted to strings, and all major browsers support the toString method, but it cannot set delimiter styles.

Example: Define an array, click the button to convert the array into a string, and display the type and result on the page.

<script type="text/javascript">
  function myFunction(){
   var animal = ["dog", "cat", "elephant", "tiger"];
   var str=animal.toString();
   document.write("类型是:"+typeof(str)+",字符串是:"+str);   
  }
 </script>
Copy after login

The effect of the toString() method is the same as the picture above.

The above introduces two methods to convert JS arrays into strings, one is the join() function method, and the other is the toString method, but the join() method can set the style of the separator, and the toString method The default is comma delimiter. The specific method you choose at work depends on your personal habits and project needs. Beginners can try it by themselves. I hope this article will be helpful to you! For more related tutorials, please visit JavaScript Video Tutorial php Public Welfare Training

The above is the detailed content of Detailed graphic explanation of two methods of converting arrays to strings in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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