user605334
user605334

Reputation:

How do I compare string and boolean in Javascript?

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]

Upvotes: 24

Views: 60476

Answers (8)

Yukulélé
Yukulélé

Reputation: 17062

  • 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

Upvotes: 24

gifpif
gifpif

Reputation: 4917

if(data+''=='true'){
    alert('true');
}  

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

Upvotes: 0

alex
alex

Reputation: 490163

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.

Upvotes: 18

Yura Galavay
Yura Galavay

Reputation: 456

var data = true;
data === "true" //false
String(data) === "true" //true

This works fine.

Upvotes: 13

simplyharsh
simplyharsh

Reputation: 36373

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.

Upvotes: 4

AndrewR
AndrewR

Reputation: 6748

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 }

Upvotes: 1

Larry K
Larry K

Reputation: 49104

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.

Upvotes: 4

jnpdx
jnpdx

Reputation: 52347

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

Upvotes: 0

Related Questions