0

When I tried with additions of variables I saw that:

https://jsfiddle.net/tyfyLsw9/

I think it's because this doesn't contain an integer.

  var month = $("#monthd").val();
  var J = 1;
  var D = 8;
  var K = J + D;
  var U = J + month;

As you can see in fiddle J + month returns 110 instead of 11, why?

Kira
  • 1,363
  • 15
  • 45
ShotNet
  • 47
  • 3

1 Answers1

1

its a string, so the number you are adding gets coerced into a string as well. "10" + "1" = "101";

simply wrap the value returned in a Number Construct

var month = Number($("#monthd").val());

additionally you can use parseInt if the values are integers.

var month = parseInt($("#monthd").val(), 10);

the , 10 is important to parse it with base 10.

Bamieh
  • 9,317
  • 4
  • 30
  • 51