How do I compare string and boolean in Javascript?

后端 未结 9 1251
长情又很酷
长情又很酷 2020-12-11 01:16

I got the Json \"false\" from server. I respond as bool but it\'s Json so it\'s in browser type is String instead of bool

9条回答
  •  自闭症患者
    2020-12-11 01:31

    String.prototype.revalue= function(){
      if(/^(true|false|null|undefined|NaN)$/i.test(this)) return eval(this);
      if(parseFloat(this)+''== this) return parseFloat(this);
      return this;
    }
    

    From: http://www.webdeveloper.com/forum/showthread.php?t=147389

    Actually, you just need the first "if" statement from the function -- tests to find true or false in the code and the evals it, turning it into the boolean value

提交回复
热议问题