0

The title says it all.

I've just seen someone creating a new array as such:

var container = new Array(+max);

Where max was an integer. I've tested and it works. I've also tested without the + symbol and it also works, so why is it there? Is there any specific reason?

Ted
  • 590
  • 4
  • 22
  • 2
    Doesn't make much sense, unless `max` is a string like `+'5' === 5` – elclanrs May 31 '16 at 17:14
  • That was exactly the case and I realized it myself just 10 seconds after asking this question. I'll answer to my own question, maybe someone will encounter the same and take longer to understand. – Ted May 31 '16 at 17:15

1 Answers1

0

Answering my own question, found solution seconds later, couldn't find it on stackoverflow, leaving it for the future.

In some cases, the max might be a string, and thus adding the + mutates it into a number if the object(string) is composed only of numbers. parseInt(max) would yield the same result.

Ted
  • 590
  • 4
  • 22