javascript (+) sign concatenates instead of giving sum?

后端 未结 7 526
旧巷少年郎
旧巷少年郎 2020-12-07 03:41

I created a simple program that make the sum of two numbers BUT.. the program is concatenating instead, This is so confusing! Can anyone help?

7条回答
  •  盖世英雄少女心
    2020-12-07 04:25

    Javascript takes dom elements as string only by default. To make mathematical calculation, you need to typecast it to integer/float or convert to number.

    parseInt(number) = number, truncates after decimal value
    parseFloat(number) = number with decimal values
    Number(number) = number with or without decimal
    

提交回复
热议问题