2

I have an array

let arr = [12,12,43,53,56,7,854,3,64,35,24,67]

i want the result back as string

let strArr = "12,12,43,53,56,7,854,3,64,35,24,67"

Please some one suggest me any solution

Mihai Alexandru-Ionut
48.6k14 gold badges106 silver badges132 bronze badges
asked Apr 10, 2017 at 8:22
1
  • just make arr + "" Commented Apr 10, 2017 at 9:19

5 Answers 5

1

You can use toString() method:

let arr = [12,12,43,53,56,7,854,3,64,35,24,67];
arr = arr.toString();
console.log(arr);
console.log(typeof arr);

You can read more about this here .

answered Apr 10, 2017 at 8:23
Sign up to request clarification or add additional context in comments.

2 Comments

Felicitari for this nice solution. +1
@Alexandru-IonutMihai, multumesc :). Same goes for your answer. :)
1

One solution is to use join method.

The join() method joins the elements of an array into a string, and returns the string.

let arr = [12,12,43,53,56,7,854,3,64,35,24,67]
let strArr = arr.join();
console.log(strArr);

answered Apr 10, 2017 at 8:25

Comments

1

Use Array.prototype.join() .

The join() method joins all elements of an array (or an array-like object) into a string.

var a = [12,12,43,53,56,7,854,3,64,35,24,67];
a.join(); // '12,12,43,53,56,7,854,3,64,35,24,67'
answered Apr 10, 2017 at 8:27

Comments

1

JS type coercion is sometimes useful.

var arr = [12,12,43,53,56,7,854,3,64,35,24,67],
strArr = arr + ""; // <- "12,12,43,53,56,7,854,3,64,35,24,67"
answered Apr 10, 2017 at 9:22

Comments

1

Solution to this would be to use join()

let arr = [12,12,43,53,56,7,854,3,64,35,24,67]
let strArr = arr.join();

Second you be to use toString()

 let arr = [12,12,43,53,56,7,854,3,64,35,24,67]
 let strArr = arr.toString();

Because you want to join by a comma, they are basically identical, but join allow you to chose a value separator.

Mihai Alexandru-Ionut
48.6k14 gold badges106 silver badges132 bronze badges
answered Apr 10, 2017 at 8:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.