Tech Junkie Blog: JavaScript Array Methods: The Array.join() Method

Monday, April 9, 2018

JavaScript Array Methods: The Array.join() Method

The Array.join() method converts all of the elements of an array into strings and concatenates the elements.  If no delimiter is defined it is separated by a comma.  The most common use for this is to convert an array into a comma separated list of strings.

For example let's say we want to convert an array of zip codes into a comma separated list.

    <script>
        var zipCodes = [90210, 90211, 90212, 90213, 90214];
    </script>

If we just call the zipCodes.join() method we will get a string of comma separated list










If we want to separate the zip codes with a dash we can specify the separator like the following zipCodes.join("-")








Well you get the idea, if you want to separate your elements with a space you would type zipCodes.join(" ")


No comments:

Post a Comment