-1

I have this array

var p = [0,1,2,3,4];

and I want to convert each value in the array to string like

var p = ['0','1','2','3','4'];

any ideas, help please?

1
  • why give me a negative? drop me a comment at least. Oct 26, 2016 at 6:05

2 Answers 2

2

You can convert number to string simply by prepending empty string to it.

p = p.map(function(e){return ""+e});
2

All you need to do is pass the Number value to the String function to get a string out of a Number.

[0,1,2,3,4].map(function(value) { return String(value); });
2
  • would this cause error if the given value is already on string? Oct 26, 2016 at 6:03
  • No It wouldn't cause an error. You can pass anything to the String function
    – Ryan
    Oct 26, 2016 at 6:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.