• 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 / Remove Commas From Array in JavaScript

Remove Commas From Array in JavaScript

October 5, 2022 Leave a Comment

We can mean two things when we say that we want to remove commas from an array in JavaScript. The first is to actually remove commas that are found in an array. The second thing we can mean by this is to display the values of an array without them being separated by commas.

Let’s take a look at the second option first, displaying an array without commas. If you want to learn how to remove commas from an array, just scroll down a little to that section.

Let’s first look at a simple array and how it is displayed.

var array1 = ["This", "is", "an", "array", "of", "strings"];

console.log(array1);

#Output
["This", "is", "an", "array", "of", "strings"]

If we wanted to display the contents of this array without any commas, we simply have to use the JavaScript join() method to convert the array to a string and display it without commas.

Here is how this is easily done.

var array1 = ["This", "is", "an", "array", "of", "strings"];

console.log(array1.join(" "));

#Output
This is an array of strings

Let’s take a look at our first option of removing commas found in an array.

Remove Commas in an Array Using JavaScript

We can use JavaScript to remove commas from an array by making use of a for loop with an if conditional statement. Here is a simple function we will create to remove commas from an array.

function removeCommas(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != ',' ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

In the function above, we simply create a new array, loop through all the values of the old array, and only add values that are not commas to the new array.

Notice above we also make use of the push() method. This simply adds the element to the end of the array.

And here is our function in use with an example array.


var array_of_strings = ["This",",","is","an","array",",","with","commas",",","."];

function removeCommas(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != ',' ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

console.log(removeCommas(array_of_strings));

#Output:
['This', 'is', 'an', 'array', 'with', 'commas', '.']

There is another way we can do this that will require less code.

Using JavaScript to Remove Commas From an Array Using the Filter Method

Below we will show another way we can remove commas from an array, this time by using the filter() method.

var array_of_strings = ["This",",","is","an","array",",","with","commas","."];

var filtered_array = array_of_strings.filter(x => x != ",");

Let’s put this code in a function to make it easier to remove commas from an array.

function removeCommas(arr){
  return arr.filter(x => x != ",");
};

And now let’s give it the same example as we did to our first function above.

function removeCommas(arr){
  return arr.filter(x => x != ",");
};

var array_of_strings = ["This",",","is","an","array",",","with","commas","."];

console.log(removeCommas(array_of_strings));

// Output:
['This', 'is', 'an', 'array', 'with', 'commas', '.']

Hopefully this article has been useful for you to learn how to remove commas from an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 2.  Using JavaScript to Get a Random Number Between 1 and 5
  • 3.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 4.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 5.  Using JavaScript to Declare Multiple Variables
  • 6.  Using JavaScript to Reverse a Number
  • 7.  Remove Vowels From String In JavaScript
  • 8.  How to Swap Values in an Array in JavaScript
  • 9.  How to Find the Longest String in an Array in JavaScript
  • 10.  Using JavaScript to Check if Number is Divisible by Another Number

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

x