method return True which is not boolean type

This question already has an answer here:

  • How can I convert a string to boolean in JavaScript? 71 answers

  • Use JSON.parse() . Takes a well-formed JSON string and returns the resulting JavaScript value.

    var str = data.toLowerCase();
    
    if(JSON.parse(str)){
    
      // your code
    }
    

    Note:

    Using JSON.parse()

    JSON.parse('{}');              // {}
    JSON.parse('true');            // true
    JSON.parse('"foo"');           // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse('null');            // null
    JSON.parse('{"1": 1, "2": 2}') //Object {1: 1, 2: 2}
    
    链接地址: http://www.djcxy.com/p/75080.html

    上一篇: 字符串到布尔值

    下一篇: 方法返回True,它不是布尔类型