168

Possible Duplicate:
array join() method without a separator

I'm using .join() to convert my array to a string so I can output it in a text box as the user selects numbers in a calculator, I'm not entirely sure how I can remove the commas that are also being output in the list however. Can someone advise how this can be achieved or if there is a different approach I should be using?

JS

$(document).ready(function() {
    var total = 0;
    var arr = [];

    //Testing
    $('#calculator').children('.num').on('click', function(e) {
        var clickedNumber = $(this).data('id');
        arr.push(clickedNumber);
        console.log(arr.join());
        e.preventDefault();
    });
});​

JS Fiddle http://jsfiddle.net/CVr25/

3
  • 2
    .join is a native array method, it's not part of jQuery. Commented Aug 26, 2012 at 17:16
  • @FelixKling: Shouldn't it rather be named "…without a separator"? :-) Commented Aug 26, 2012 at 17:25
  • @Bergi: Probably :) You can edit it ;) Commented Aug 26, 2012 at 17:28

4 Answers 4

359

Simply like that:

arr.join("")
Sign up to request clarification or add additional context in comments.

Comments

47

You can specify an empty string as an argument to join, if no argument is specified a comma is used.

 arr.join('');

http://jsfiddle.net/mowglisanu/CVr25/1/

Comments

39

The .join() method has a parameter for the separator string. If you want it to be empty instead of the default comma, use

arr.join("");

Comments

24

All you need to do is :

arr.join('');

FIDDLE

Comments

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.