• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript join – Create String from Elements in an Array

JavaScript join – Create String from Elements in an Array

April 21, 2022 Leave a Comment

We can use the JavaScript join() method to convert an array of values into a string.

var arrayOfColors = ["Red", "Green", "Blue", "Orange", "Yellow"];
var stringOfColors = arrayOfColors.join();

Let’s take a look at a simple example below.


Let’s say we have a simple array of colors and we want to convert the array to a string. To do this we simply need to use the JavaScript Array join() method.

var arrayOfColors = ["red","blue","green","yellow","orange","purple","pink","black"];
var stringOfColors = arrayOfColors.join();

In this example, the resulting string “stringOfColors” would now contain the following:

red,blue,green,yellow,orange,purple,pink,black

If you notice the string has no spaces between the colors. If we wanted to convert the array to a string and have there be spaces in between each color, we would just have to add the following code to the join() method:

var arrayOfColors = ["red","blue","green","yellow","orange","purple","pink","black"];
var stringOfColors = arrayOfColors.join(", ");

Which would result in the following:

red, blue, green, yellow, orange, purple, pink, black

Let’s take a look at an example using HTML.

Using the JavaScript join method to convert an Array into a String with a Click

In this example, we will have a large array of CSS color names that are supported by most browsers. We will take the long array of colors, and convert it into a string of color names, and then show it to the user.

Here is the HTML set up:

<div id="div1">
  <div id="click-me" onclick="showColors()">Show colors</div>
  <div id="results"></div>
</div>

In the JavaScript portion of this example, all we will do is grab the array of colors that we created, and use the join() method on it to convert it into a string. We will then show the string of colors to the user using the textContent property.

Here is the JavaScript code:

var cssColors = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","RebeccaPurple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"];

function showColors() {
  //Convert our array of colors above into a string of colors, with each color separated by a comma and space.
  var stringOfColors = cssColors.join(", ");
  //Show colors to the user
  document.getElementById("results").textContent = stringOfColors;
}

The final code and output for this example of using the JavaScript join method is below:

Code Output:

Show colors

Full Code:

<div id="div1">
  <div id="click-me" onclick="showColors()">Show colors</div>
  <div id="results"></div>
</div>

<script>

var cssColors = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","RebeccaPurple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"];

function showColors() {
  var stringOfColors = cssColors.join(", ");
  document.getElementById("results").textContent = stringOfColors;
}

</script>

Hopefully this article has been useful for you to understand how to use the JavaScript join() method.

Other Articles You'll Also Like:

  • 1.  Using Javascript to Check if Value is Not Null
  • 2.  Using JavaScript to Stop a Timer
  • 3.  JavaScript Square Root with Math.sqrt() Method
  • 4.  Get the Size of a Set in JavaScript
  • 5.  Using Javascript to Add Class to Element
  • 6.  Check if Character is Uppercase in JavaScript
  • 7.  Using JavaScript to Set the Width of a Div
  • 8.  Using JavaScript to Capitalize the First Letter of a String
  • 9.  Using JavaScript to Get Radio Button Value
  • 10.  Remove Special Characters From String in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy