0

I don't know if this is a bug, but it feels kind of strange. Can you store a boolean returned by a function in a variable using javascript? Whenever I try to store a boolean value in a variable which is returned by a function, it gets changed to string.

I have the following function which converts a string to a boolean

function str2bool(strvalue){
  console.log("Value is - " + strvalue);
  console.log("Type is - " + typeof strvalue);
  return (strvalue && typeof strvalue == 'string') ? (strvalue.toLowerCase() == 'true') : (strvalue == true);
}

I've found this function somewhere here on StackOverflow, but I do not remember it's author, so if you are reading this, sorry about me not giving proper credits:)

I have another javascript line, which looks as follows:

target.prop('disabled',str2bool(booleanInStringFormat));
console.log("Typeof str2bool return is - " + typeof str2bool(booleanInStringFormat));

If I use it this way, everyting works fine, the str2bool function returns the following lines to the console:

Value is - false
Type is - string

And the line after the main function returns:

Typeof of str2bool function is - boolean

But if I try to store the return value of str2bool in a variable, and use it afterwards, the prop function won't work, because apparently the variable that I use to store the return value of str2bool becomes a string. If I run this code I get the following results:

status = str2bool(booleanInStringFormat);
console.log("Typeof status is - " + typeof status);
target.prop('disabled',status);

The results are the following:

Value is - false
Type is - string
Typeof status is - string
End result is that target remains disabled

So, why is the typeof the variable in which I store the return of the function is changed back to string?

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68

5 Answers5

1

Because you use global variable status, which appeared to be a property of the global object window, this property could only be string.

The window.status.

You could just change to an other variable name, but much better, avoid using global variable.

(function(){
  var status = str2bool('false');
  console.log(typeof status);
}());
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Yep, this was the problem... thanks for the prompt answer!:) What is the global status variable for btw? Thanks:) Will mark this as correct in a few minutes:) – Adam Baranyai Apr 22 '15 at 16:13
  • @AdamBaranyai It is used to set the text in the status bar at the bottom of the browser. – xdazz Apr 22 '15 at 16:15
1

Tried the below snippet, it seems we can return a boolean and save it

function booleanReturnCheck(){
   return false;
}

var isBool = booleanReturnCheck();
console.log(isBool);
console.log(typeof (isBool));

JSFIDDLE

500KD
  • 46
  • 5
1

The accepted answer on this link discusses boolean variables. Maybe something there might help: Declaring a boolean in JavaScript using just var

The section to pay attention to is

var IsLoggedIn1 = "true"; //string
var IsLoggedIn2 = 1; //integer
var IsLoggedIn3 = true; //bool

It seems that your values are strings because they are being set as strings.

Community
  • 1
  • 1
kjw
  • 1,459
  • 3
  • 17
  • 24
0

you can parse bool using !!

var bool = !!'true';
Joe Fitter
  • 1,309
  • 7
  • 11
0

You can parse a Boolean Variable using :

  1. var myVar = Boolean("false");
  2. var myVar = !!"false";

Note that any string which isn't the empty string or "false" will evaluate to "true".

Abdessamad Doughri
  • 1,324
  • 2
  • 16
  • 29