Convert array to string javascript

Example

Convert an array to a string:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.toString[];

Try it Yourself »

Definition and Usage

The toString[] method returns a string with array values separated by commas.

The toString[] method does not change the original array.

Note

Every JavaScript object has a toString[] method.

The toString[] method is used internally by JavaScript when an object needs to be displayed as a text [like in HTML], or when an object needs to be used as a string.

Normally, you will not use it in your own code.

Syntax

Parameters

Return Value

Type Description
A string The array values separated by commas.

Browser Support

toString[] is an ECMAScript1 [ES1] feature.

ES1 [JavaScript 1997] is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

Examples

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join[];

Try it Yourself »

Another separator:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join[" and "];

Try it Yourself »

Definition and Usage

The join[] method returns an array as a string.

The join[] method does not change the original array.

Any separator can be specified. The default is comma [,].

Syntax

Parameters

Parameter Description
separator Optional.
The separator to be used.
Default is a comma.

Return Value

Type Description
A string. The array values, separated by the specified separator.

Browser Support

join[] is an ECMAScript1 [ES1] feature.

ES1 [JavaScript 1997] is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

Four methods to convert an array to a string.

Coercing to a string

var arr = ['a', 'b', 'c'] + [];  // "a,b,c"

var arr = ['a', 'b', 'c'] + '';  // "a,b,c"

Calling .toString[]

var arr = ['a', 'b', 'c'].toString[];  // "a,b,c"

Explicitly joining using .join[]

var arr = ['a', 'b', 'c'].join[];  // "a,b,c" [Defaults to ',' seperator]

var arr = ['a', 'b', 'c'].join[','];  // "a,b,c"

You can use other separators, for example, ', '

var arr = ['a', 'b', 'c'].join[', '];  // "a, b, c"

Using JSON.stringify[]

This is cleaner, as it quotes strings inside of the array and handles nested arrays properly.

var arr = JSON.stringify[['a', 'b', 'c']];  // '["a","b","c"]'

The toString[] method returns a string representing the specified array and its elements.

Try it

Syntax

Return value

A string representing the elements of the array.

Description

The Array object overrides the toString method of Object. The toString method of arrays calls join[] internally, which joins the array and returns one string containing each array element separated by commas. If the join method is unavailable or is not a function, Object.prototype.toString is used instead, returning [object Array].

const arr = [];
arr.join = 1; // re-assign `join` with a non-function
console.log[arr.toString[]]; // Logs [object Array]

console.log[Array.prototype.toString.call[{ join: [] => 1 }]];  // Logs 1

JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.

Examples

Using toString[]

const array1 = [1, 2, 'a', '1a'];

console.log[array1.toString[]];
// expected output: "1,2,a,1a"

Using toString[] on sparse arrays

Following the behavior of join[], toString[] treats empty slots the same as undefined and produces an extra separator:

console.log[[1, , 3].toString[]]; // '1,,3'

Specifications

Specification
ECMAScript Language Specification
# sec-array.prototype.tostring

Browser compatibility

BCD tables only load in the browser

See also

Can you convert array to string in JavaScript?

JavaScript Array toString[] The toString[] method returns a string with array values separated by commas.

How do I convert an array to a string?

toString[] method: Arrays. toString[] method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets [“[]”]. Adjacent elements are separated by the characters “, ” [a comma followed by a space].

Which method converts an array to string in JavaScript?

toString[] The toString[] method returns a string representing the specified array and its elements.

What does .split do in JavaScript?

The split[] method splits a string into an array of substrings. The split[] method returns the new array. The split[] method does not change the original string. If [" "] is used as separator, the string is split between words.

Chủ Đề