13

Can you explain why the if condition doesn't work without the eval function:

var myBoolean= document.getElementById("someBoolean").value;  //This is a 'false'

if(myBoolean)
{ 
  alert(Your boolean is True);  //This condition always getting executed even though myBoolean is false;
}

if(eval(myBoolean))
{
 alert("You will never see this alert bcoz boolean is false");
}
3
  • 1
    How does document.getElementById("someBoolean"); return 'false'? Commented Dec 1, 2011 at 22:55
  • Hi Rocket, Thats an input field with false as the value. Commented Dec 1, 2011 at 22:56
  • 1
    document.getElementById("someBoolean"); returns a DOM element (or null). To get the value you'd have to do document.getElementById("someBoolean").value;. Commented Dec 1, 2011 at 22:57

7 Answers 7

15

In Javascript the following values are treated as false for conditionals:

  • false
  • null
  • undefined
  • The empty string ''
  • The number 0
  • The number NaN

Everything else is treated as true.

'false' is none of the above, so it's true.

Sign up to request clarification or add additional context in comments.

Comments

5

The string 'false' evaluates to the boolean true

10 Comments

document.getElementById("someBoolean") does not return 'false' if the element is present in the html it return the element and that evaluates to true
@user1052591: Strings in JavaScript convert to true unless they are empty ('').
@TimWickstrom.com: It does evaluate to the String 'false'. He's getting the value of the element, not the element itself. Look, he's using .value but he should be using .val() instead.
@JZ11 the question was updated, the original question did not have .value
@Davy8 Well I didn't see that it was edited. Regardless, his comment says he's expecting the String 'false'. If I was more careful I would have told him to use .val() but regardless, the point of the question is that a non-empty String evaluates to the boolean true
|
2

This is because it's not actually a boolean, it's a the string 'false'. When you convert a string to a boolean, '' is false and anything else is true.

You check if it's equal to the string 'false' (or 'true') or not.

var myBoolean = 'false'; // (string)
myBoolean = myBoolean !== 'false'; //false (boolean)

2 Comments

var myBoolean = document.getElementById("someBoolean"); Will never equal 'false' as a string or boolean false. It will === null or true as it will eval as an dom element
@TimWickstrom.com: Yeah, I realized that, that's why I changed my answer, and also pointed it out to the OP.
1

'false' == true, crazily enough because of JavaScript's implicit type coercion. Check out these other examples from Crockford's The Elements of JavaScript Style.

'' == '0' // false
0 == '' // true

0 == '0' // true

false == 'false' // false
false == '0' // true

false == undefined // false
false == null // false

null == undefined // true

' \t\r\n ' == 0 // true

You could solve this particular problem by changing your code to something like

var myBoolean = document.getElementById("someBoolean").value === "true"

Also, it is almost always better to use !! and === rather than ! and ==

2 Comments

Hi Alex, So doing it the eval(myBoolean) is correct or there is a best/better solution.
As I said in my answer I would go with var myBoolean = document.getElementById("someBoolean").value === "true" or some variation.
0

document.getElementById("someBoolean") does not return a boolean true/false it returns an element or undefined / null

you could reverse your logic and get the expected result:

    if(!myBoolean)
    { 
      alert('This element does not exist');
    }

    if(!eval(myBoolean))
    {
     alert("Do not know why you would ever want to do this");
// you could do typeof() 
    }

1 Comment

The question was updated so this answer is no longer relevant
0

Try:

var myBoolean= document.getElementById("someBoolean").value;  //This is a 'false'

if(myBoolean != "false")
{ 
  alert(Your boolean is True);  //This condition always getting executed even though myBoolean is false;
}

Like others have said, a string isn't a boolean value, so using it as though it was will give you a logical error.

Comments

-1

A string IS a boolean truth value according to ECMA

var a = ""<br/>
a&&false //-> false<br/>
a||false //-> "" (truthy)<br/>

2 Comments

Well 4 years after the discussion started might mean that versions of ECMA / JavaScript and the language rules changed, but empty string truthy? I read question and accepted answer as that there was a misconception starting with varaible names, where - as in some programming languages - the string 'false' is considered like a true value in boolean contexts. whatever && false should always be false regardless of whatever brings to the table, and yes, the effective value of ""||false may be "", but I would understand this as "false" in a boolean context.
This answer does not provide anything the other answers to the question already have provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.