Table of contents
- 1. Summary
- 2. Syntax
- 3. Parameters
- 4. Description
- 5. See also
Summary
Returns a string representing the specified array and its elements.
Method of Array | |
|---|---|
| Implemented in | JavaScript 1.1 |
| ECMAScript Edition | ECMAScript 1st Edition |
Syntax
array.toString()
Parameters
None.
Starting in JavaScript 1.8.5, and consistent with ECMAScript 5th edition semantics, the toString() method is generic and can be used with any object. If the object has a join() method, it will be called and that value will be returned. Otherwise Object.prototype.toString() will be called, and the resulting value will be returned.
Description
The Array object overrides the toString method of Object. For Array objects, the toString method joins the array and returns one string containing each array element separated by commas. For example, the following code creates an array and uses toString to convert the array to a string.
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr']; var myVar = monthNames.toString(); // assigns "Jan,Feb,Mar,Apr" to myVar.
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.