• 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
  • VBA
  • 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.  How to Swap Values in an Array in JavaScript
  • 2.  How to Split a Number into Digits in JavaScript
  • 3.  Using JavaScript to Increment a Variable by 1
  • 4.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 5.  How to Generate a Random Letter In JavaScript
  • 6.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 7.  Using JavaScript to Get the Max Value in an Array
  • 8.  JavaScript Print Array – Printing Elements of Array to Console
  • 9.  Using JavaScript to Check if a Number is Divisible by 2
  • 10.  Using JavaScript to Get the Height of an Element

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy