JavaScript Glossary - Lesson #6

JavaScript Array .toString() Method

This method returns a String representation of the elements withing the calling array. This method is somewhat similar to the join() method. It returns a string value of all the array elements separated by a comma (,).

js
['a', 'boy', 'from', 'mars'].toString()
// 'a , boy, from, mars'
js
// Array of names
const names = ['john', 'chris', 'mike', 'kap', 'will']

// Convert to string
const stringNames = names.toString()

console.log(stringNames)
// output: john,chris,mike,kap,will
js
[1, 2, 3, 4, 'boy'].toString()

A single string representation of all the values contained in the array

js
// Create array with strings
const values = ['1', '2', '3', '4'];

// Convert array to string separated by a comma
const string = values.toString();

console.log(string);
// output: 1,2,3,4

Want to improve your coding and design skills?

I'm continually researching the best practices and tools for coding.
Join 50,000+ developers looking to make cool stuff.

We value your privacy. 1-click unsubscribe.

Chris Sev

Chris Sev

Chris Sev is the co-founder of Better Dev. Coding better every day. Previously he created Scotch.io which was acquired.

Comments

What did you think of the article? Let us know!
(these comments are powered by GitHub issues and use 0 trackers)