• 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 / Convert an Array of Values into a String Without Commas in JavaScript

Convert an Array of Values into a String Without Commas in JavaScript

May 4, 2022 Leave a Comment

In JavaScript, we can convert an array of elements into to string without commas easily using the Array join() method.

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

The join method will convert an array of values into a string for us just using the join() method as is. This will create a string with the value of the array in it separated by commas. If we want to get rid of the commas, and just have the value separated by spaces, we would use the code above, 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 without commas. 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

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

Converting an Array of Values into a String Without Commas with a Click

In this example, we will have a large array with the elements each containing a string. We will take the long array of text, and convert it into one long string, and then show it to the user.

Here is the HTML set up:

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

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

The text we will use in our array will just be text we take from our about page.

Here is the JavaScript code:

var aboutText = ["This", "blog", "is", "a", "compilation", "of", "a", "programmer’s", "findings", "in", "the", "world", "of", "software", "development", ", website", "creation,", "and", "automation", "of", "processes."];

function showText() {
  //Convert our array of text above into a string with each item of the array separated by a space.
  var newText = aboutText.join(" ");
  //Show text to the user
  document.getElementById("results").textContent = newText;
};

The final code and output for this example is below:

Code Output:

Show text

Full Code:

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

<script>

var aboutText = ["This", "blog", "is", "a", "compilation", "of", "a", "programmer’s", "findings", "in", "the", "world", "of", "software", "development", ", website", "creation,", "and", "automation", "of", "processes."];

function showText() {
  var newText = aboutText.join(" ");
  document.getElementById("results").textContent = newText;
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to convert an array to string without commas.

Other Articles You'll Also Like:

  • 1.  Remove String From Array in JavaScript
  • 2.  JavaScript Change Background Color of Element
  • 3.  Remove the Last Element From Array in JavaScript
  • 4.  How to Remove Decimals of a Number in JavaScript
  • 5.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 6.  Using JavaScript to Check If String Contains Only Certain Characters
  • 7.  Using JavaScript to Replace Space With Dash
  • 8.  Using JavaScript to Remove Leading Zeros
  • 9.  JavaScript slice – How to Get a Substring From a String
  • 10.  Using JavaScript to Replace a Character at an Index

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