Boolean variable returns as string from javascript function
You don't declare the status
status variable.
Therefore, the global one (window.status
) is overwritten.
However, the HTML 5 spec defines that property as a DOMString:
interface Window : EventTarget {
attribute DOMString status;
};
Therefore, it has a setter (either exposed or internal) which stores the stringified value.
To fix it, just declare your local variable using the var
statement.
Can't store a boolean returned by a function in a variable
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);
}());
Returning a boolean value in a JavaScript function
You could simplify this a lot:
- Check whether one is not empty
- Check whether they are equal
This will result in this, which will always return a boolean. Your function also should always return a boolean, but you can see it does a little better if you simplify your code:
function validatePassword()
{
var password = document.getElementById("password");
var confirm_password = document.getElementById("password_confirm");
return password.value !== "" && password.value === confirm_password.value;
// not empty and equal
}
Function return boolean statement
Your code example is using result; 'true'
(for example) to indicate a true
result. This doesn't do anything - in fact it's not correct at all.
Instead it should use return true
:
let response;
function isOldEnoughToVote(age) {
if (age >= 18) {
return true;
} else {
return false;
}
}
console.log(isOldEnoughToVote(10));
console.log(isOldEnoughToVote(18));
console.log(isOldEnoughToVote(50));
Javascript Use a returned value for a boolean comparison
function Function2(){
if(Function1() == true){
console.log("Hello")
}
}
only replace Function2
with Function2()
JS simple boolean statement in if function - always getting TRUE value
In global scope, status
refers to the built-in global variable window.status
. Every value assigned to it will be converted to a string:
status = false;
console.log(status, typeof status);
Related Topics
Firestore: What's the Pattern for Adding New Data in Web V9
Using Number as "Index" (JSON)
How to Sort an Array of Objects Based on the Ordering of Another Array
What's the Best Way to Loop Through a Set of Elements in JavaScript
Jquery .Load()/.Ajax() Not Executing JavaScript in Returned HTML After Appended
JavaScript Accessing Inner Dom of Svg
How to Use JavaScript or Jquery to Read a Pixel of an Image When User Clicks It
<Iframe> JavaScript Access Parent Dom Across Domains
Google Maps API - Getting Closest Points to Zipcode
About Closure, Lexicalenvironment and Gc
Using Ajax to Read Local Files
JavaScript Can't Access Private Properties
Onclick Event Not Firing on Jsfiddle.Net
Javascript: Simple Way to Check If Variable Is Equal to One of Two or More Values
How to Sort an Array Without Mutating the Original Array
Clearrect Function Doesn't Clear the Canvas
How to Consume Http Component Efficiently in a Service in Angular 2 Beta