-1

New to JS and VUE.

I have a variable numberOfItems in my data which has 10 as its value.

I need to compare if this value is equal to the sum of 5 different variables data

var numberOfItems = 10
var destinations = (this.campaign.shipments_ue + this.campaign.shipments_uk + this.campaign.shipments_islas + this.campaign.shipments_peninsula + this.campaign.shipments_international)

I get 22222 in destinations value instead of 10, what method should I use to sum them up so I can compare them?

Thanks!

Gibson
  • 2,055
  • 2
  • 23
  • 48

1 Answers1

0

Observation : Looks like you are adding a string instead of number.

If data type is string then it is resulting of concatenating strings not a number.

For Ex :

var num1 = '10', num2 = '20';
console.log(num1 + num2); // 1020 (Here both the variables are string)

num1 = 10;
console.log(num1 + num2); // 1020 (Here variable 1 is number but variable 2 is a string)

num2 = 20;
console.log(num1 + num2); // 30 (Here both the variables are number)

Now, As per your requirement. You can use parseInt() function which helps in parsing a string argument and returns an integer.

Demo :

var num1 = '10', num2 = '20';

console.log(parseInt(num1) + parseInt(num2));
Debug Diva
  • 26,058
  • 13
  • 70
  • 123