24

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.

So if I run (!data) whenever I want to check "false" == false then they not worked.

So how can I parse bool from String in JavaScript then?

"true" == true and "false" == false. Then the code (!data) can check what it is [true and false]

8 Answers 8

24
  • If one of the operands is a boolean, convert the boolean operand to 1 if it is true and +0 if it is false.
  • When comparing a number to a string, try to convert the string to a numeric value.

from MDN Equality Operators page

Examples:

true == "true";   // 1 == NaN → false
true == "1";      // 1 == 1   → true
false == "false"; // 0 == NaN → false
false == "";      // 0 == 0   → true
false == "0";     // 0 == 0   → true
Sign up to request clarification or add additional context in comments.

Comments

18

I would just explicitly check for the string "true".

let data = value === "true";

Otherwise you could use JSON.parse() to convert it to a native JavaScript value, but it's a lot of overhead if you know it's only the strings "true" or "false" you will receive.

5 Comments

JSON.parse() will decode the server boolean to javascript boolean, so you can call (!myJSONObject.myBool) it's more recomended than the eval. json.org
This is really overkill. It is at least as efficient to simply test the string value against "true". Besides being perfectly sufficient for his needs, it's also universally portable.
@chomp It does answer the question How can I parse bool from string in JavaScript? I would simply check for the string 'false', however.
At this point it becomes a petty argument, since it is no more valid to suggest that "banana" is true than it is to suggest that "banana" is false. :)
this thing i found later because project not have this one [json.js]. i use jQuery.parseJSON(). thanks
13
var data = true;
data === "true" //false
String(data) === "true" //true

This works fine.

Comments

4

Try expression data == "true"

Tests:

data = "false" -- value will be false

date = "true" -- value will be true

Also, fix your JSON. JSON can handle booleans just fine.

Comments

4

If its just a json "false"/"true", you can use,

if(! eval(data)){
    // Case when false
}

It would be more cleaner, if you restrict the code to accept only JSON data from server, and always jsonParse or eval it to JS object (something like jquery getJSON does. It accepts only JSON responses and parse it to object before passing to callback function).

That way you'll not only get boolean as boolean-from-server, but it will retain all other datatypes as well, and you can then go for routine expressions statements rather than special ones.

Happy Coding.

Comments

1

I think you need to look at how the JSON data is being generated. You can definitely have a normal JS boolean false in JSON.

{ "value1" : false, "value2" : true }

Comments

0
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

2 Comments

Adding functions to a base class (such as String) tends to raise maintainability and clarity issues.
While clever - and maybe I'm just a stickler for type safety - I would never in a million years encourage anyone to use this code for any practical purpose, ever.
0
if(data+''=='true'){
    alert('true');
}  

Convert boolean to string by appending with blank string. and then compare with Stringobject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.