5

I was comparing two branches and there is a divergence in code while the + operator, in my opinion it doesn't make any difference since it's push.
Is there any difference?

Before

if (numberPattern.test(val)) {
            var getNumbers = val.match(numberPattern);
            for (i = 0; i < getNumbers.length; i++) {
                valores.push(getNumbers[i])
            }
        }

After

if (numberPattern.test(val)) {
            var getNumbers = val.match(numberPattern);
            for (i = 0; i < getNumbers.length; i++) {
                valores.push(+getNumbers[i])
            }
        }
Daniela Morais
  • 2,125
  • 7
  • 28
  • 49

2 Answers2

7

It is converting it to a Number, where the other case is leaving it as as string.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

+ will actually change getNumbers[i] to a number type. The +(unary operator) is actually used to convert it to a number.

Try running this code:

var s = "1"; //String
var s1 = +s; //String changed to a number now
console.log(typeof s1);

You will see the type of s1 will be number.

Zee
  • 8,420
  • 5
  • 36
  • 58