1

How to convert arraylist to string in Javascript?

I just want to convert arraylist to string in Javascript. I have this code.

var digitStack = [];
digitStack.push(1);
digitStack.push(2);
digitStack.push(3);

and I want to make "123". Any good ideas do you have?

3 Answers 3

4

You can use the Array.join() with an empty string as the separator

var digitStack = [];
digitStack.push(1);
digitStack.push(2);
digitStack.push(3);
var string = digitStack.join('');

snippet.log(string)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

Comments

2

Just use Array.join() :

var digitStack = [];
digitStack.push(1);
digitStack.push(2);
digitStack.push(3);

var digitStr = digitStack.join('');

console.log(digitStr); // gives you a string.

Comments

1

User Arrayname.join();

var digitStack = [];
digitStack.push(1);
digitStack.push(2);
digitStack.push(3);

var digitStr = digitStack.join(''); //predefined function

document.write(digitStr); // just to verify the string, it has to be removed later

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.