Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array toString() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array toString() Method

The Array.toString() method is a built-in JavaScript function that converts an array into a comma-separated string. It is a simple way to represent the contents of an array as a string, with each element of the array joined by a comma.

Syntax

arr.toString();

Parameters

The toString() method does not take any parameters.

Return Value

The method returns a string representation of the array, where all elements are joined by a comma (,).

Example 1: Basic Array Conversion

let arr = ['HTML', 'CSS', 'JavaScript', 'jQuery'];
let str = arr.toString();
console.log(str); // Output: "apple,banana,cherry"
console.log(arr); // Output: ['apple', 'banana', 'cherry']

Example 2: Numeric Array

let arr = [10, 20, 30, 40, 50];
let str = arr.toString();
console.log(str); // Output: "10,20,30,40,50"

Example 3: Handling Mixed Data Types

let arr = ['hello', 42, true, null];

let str = arr.toString();
console.log(str); // Output: "hello,42,true,"

Example 4: Nested Arrays

let arr = [1, [2, 3], [4, 5]];
let str = arr.toString();
console.log(str); // Output: "1,2,3,4,5"

Example 5: Using toString() with Objects

let obj = [
    { name: 'Alice' },
    { name: 'Bob' },
    { name: 'Charlie' }
];

let str = obj.toString();
console.log(str); 
// Output: "[object Object],[object Object],[object Object]"

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari1+
Edge12+
Opera4+
Internet Explorer5.5+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array toSpliced() Method
Next Post: JavaScript Array unshift() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme