3

I have this code in JS:

var array = ['one', 'two'];

And each time I want to display the array, it is displayed with like this:

one,two

Now my question is, is there any way to remove that nasty comma?

4
  • You want it displayed as "onetwo"? Or... "one two" with a space Commented Jul 24, 2012 at 18:34
  • 1
    Can you share the code you're using to display the array? Commented Jul 24, 2012 at 18:35
  • 4
    To remove a coma, you'd better visit a medic. Thank god, it turned out to be a comma! Commented Jul 24, 2012 at 18:35
  • that is funny:))) Commented Feb 20, 2023 at 15:42

2 Answers 2

14

To display them next to each other, you can use join. array.join(' ');

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, I want it to be displayed like this 'one two' with a space, thank you
join can take any string as a parameter which it will use to delimit values in the array. e.g. ['one', 'two', 'three'].join(' SHIZZAM! '); will give you one SHIZZAM! two SHIZZAM! three
Thank you so much for the help guys I cannot say how much you helped me! And god you are so quick! For less than 5 minutes!!!! One last stupid question tho - how can I vote the comments here, please?
@Stefany - you may not be able to vote because you are just starting on Stack Overflow... although you can accept this answer as the correct one, thats reward enough ;)
Oh thank you Tim, that was very kind of you to explain. I rewarded you already :D
1

You could use the replace method of the string object:

array.toString().replace(',',' ')

3 Comments

and what if the indexes have commas in them? This would break. Better to use join().
He asked how to remove the comma, not how to join the elements of an array. I'd use join() in this case too :)
It will not work suppose Array selIdss has values like 1,2,3 but replace will only remove first comma output will be 1;2,3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.