Course contentsShow
← JavaScript
Lesson 831 of 2,72319. ArraysFree lesson

toString() and toLocaleString() for Display

Convert arrays to string representations for display purposes.

toString() and toLocaleString() for Display

What you'll learn: How to convert arrays into readable string formats for display or logging purposes.

The Basic Purpose

JavaScript arrays have two built-in methods that transform them into strings: toString() and toLocaleString(). These are especially useful when you need to display array contents in user interfaces, logs, or debugging output. Think of them as "translators" that convert your array's complex structure into simple, readable text.

How toString() Works

toString() converts an array to a comma-separated string of its elements:

const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.toString());  // "apple,banana,cherry"

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.toString());  // "1,2,3,4,5"

const mixed = [10, 'hello', true];
console.log(mixed.toString());  // "10,hello,true"

Notice how toString() calls each element's own toString() method and joins everything with commas—no spaces.

How toLocaleString() Works

toLocaleString() is similar but respects locale-specific formatting, particularly useful for numbers and dates:

const prices = [1234.56, 789.01];
console.log(prices.toLocaleString());  
// Output depends on your locale, e.g., "1,234.56,789.01" in US

const simpleArray = ['a', 'b', 'c'];
console.log(simpleArray.toLocaleString());  // "a,b,c"

For simple strings, both methods produce identical results. The difference shines with numbers where locale matters.

When to Use Each

Use toString() for basic, consistent string conversion. Use toLocaleString() when displaying data to users where regional formatting conventions (like thousand separators) matter.

Key Takeaway: toString() and toLocaleString() convert arrays to comma-separated strings—toString() for simple conversion, toLocaleString() for locale-aware formatting.