Javascript string/integer comparisons

后端 未结 8 1025
太阳男子
太阳男子 2020-11-22 16:34

I store some parameters client-side in HTML and then need to compare them as integers. Unfortunately I have come across a serious bug that I cannot explain. The bug seems to

8条回答
  •  自闭症患者
    2020-11-22 17:14

    The alert() wants to display a string, so it will interpret "2">"10" as a string.

    Use the following:

    var greater = parseInt("2") > parseInt("10");
    alert("Is greater than? " + greater);
    
    var less = parseInt("2") < parseInt("10");
    alert("Is less than? " + less);
    

提交回复
热议问题