Boolean Variable Returns as String from JavaScript Function

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



Leave a reply



Submit