Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference in using the " + " operator to push?

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])
            }
        }
like image 304
Daniela Morais Avatar asked May 25 '15 12:05

Daniela Morais


People also ask

Does a push mutate?

The push() method changes the original array by adding an item to the end. When you add an item, you're mutating the original array.

What is the difference between push and concat?

The push() adds elements to the end of an array and returns the new length of the array. Thus your return here is invalid. The concat() method is used to merge arrays. Concat does not change the existing arrays, but instead returns a new array.

What does push () method of the array do?

push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.

Is Spread operator faster than Concat?

As you can see, spread is 50% faster for smaller arrays, while concat is multiple times faster on large arrays.


1 Answers

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

like image 114
Daniel A. White Avatar answered Sep 25 '22 23:09

Daniel A. White