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?