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

How to use tostring method to convert js array into string

醉折花枝作酒筹
Release: 2021-08-11 09:58:00
Original
2342 people have browsed it

In the previous article, we learned how to use the join method to convert the elements in the array into strings. Please see "How to use the join() method in js to convert the elements in the array into strings 》. This time we will learn about another method of converting array elements into strings. You can refer to it if necessary.

The previous article introduced how to use the join method to convert the elements in the array into a string. This time, we will talk about another method of converting the array elements into a string.

First let’s look at a little chestnut.

<script>
var arr = new Array(3); 
arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
var nums = arr.toString();
console.log(nums);
console.log(arr);
</script>
Copy after login

The result of this small example is

How to use tostring method to convert js array into string

Let’s take a look at this result. This result is the same as the result of the previous article. The first one is a String, the second one is an array. Let’s look at the code again. The first one is where we used the toString() method, and the second one is our original array. After knowing a little bit about the toString() method, let's introduce this method. The

Array object overrides the toString method of Object. For array objects, the toString method concatenates the arrays and returns a string containing each array element separated by commas.

When an array is used as a text value or a string concatenation operation is performed, its toString method will be automatically called. If the method uses a native data type as a parameter, the String object value of the native data type is returned. If the method takes two arguments, returns the string representation of the first argument in the base specified by the second argument.

This method has several syntax formats, let’s take a look.

String toString()
static String toString(int i)
Copy after login

When our syntax format is "String toString()", the return value of this method is a String object representing an Integer value. When our syntax format is "static String toString(int i)", the return value of this method is a String object representing the specified int.

That’s all. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of How to use tostring method to convert js array into string. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!