js中var变量不能直接和int做比较么

js中var变量不能直接跟int做比较么

  <script type="text/javascript" language="javascript">
  
  function topNum(){
  
   var topNum=document.getElementById("topNum").value;
   alert("hidden的值是"+topNum);
  
   if( topNum == 2){
  
   alert("置顶帖数量不能超过两个");
   return;
   }
  }
  
  </script>


第一个提示信息能弹出 值也是正确的 第二个就是进不了if 怎么回事
------解决思路----------------------
因为是String不是int 改成=="2"
------解决思路----------------------
if( +topNum == 2){
或 
if(parseInt(topNum) == 2){

------解决思路----------------------
topNum-0 == 2
减0就转换成数字了
------解决思路----------------------
javascript的if我记得==时候自己会转换的不用手动的
==不成立进不去应该是因为topNum不是"2"或者2啊