0

I just fall into a situation like that. I tried to add "10"+1 which gives me " 101" Which is obvious as I know that it concate string with numeric and convert it to string of 101 but when write "10"-1 then it gives me 9. I how is this working

karel
  • 5,489
  • 46
  • 45
  • 50
5andeep_negi
  • 25
  • 1
  • 7

1 Answers1

1

Because the - operator cannot be applied to strings (for obvious reasons - how do you subtract a string?), it's implicitly converted to a number. So this:

"10" - 1

Is converted behind the scenes to:

10 - 1

Which is obviously:

9

For more information, look up type coercion.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • so the answer will be in string or in number – 5andeep_negi Sep 25 '19 at 12:15
  • and one more thing when i m passing value from javascript using ajax to its controller(c#) sometimes it works fine but sometime it gives error or 500 which must be because of different datatype. i any one can explain it ?? – 5andeep_negi Sep 25 '19 at 12:18
  • 500 is a server error - it would be a 4xx error for a bad request (can't remember off the top of my head) – Jack Bashford Sep 25 '19 at 21:35