Take the 2-minute tour ×

I am working through the tutorials for Javascript. I was testing out the Boolean() function and the If...Else conditions.

I have a single button on a website. When I click the button it displays the current state of a boolean variable (myvar) and also the corresponding state (myvartxt) (ie. false means "OFF", true means "ON"). The reason for this is because I want to, later on, toggle an LED ON/OFF and don't want it to say false/true but rather ON/OFF.

Now the problem is when I click the button the state of the boolean (myvar) is false which is correct, BUT the myvartxt variable is ON when it should say OFF. I just don't understand. Any ideas? Thanks for your help in advance!

Test out code quickly here:

<!DOCTYPE html>
<p id="demo"></p>
<p id="demo1"></p>


<script type="text/javascript">
function myFunction()
    //Initial state
    var myvar=new Boolean(0);
    var myvartxt = "OFF";

    //display current state of Boolean
    var x=document.getElementById("demo");

    //display corresponding ON -> true, OFF -> false
    if (myvar) //INITIALLY THIS IS false (not a string) so why does it think this is true?
        myvartxt = "ON"; 
        myvartxt = "OFF";

    var y=document.getElementById("demo1");


share|improve this question
I should also add that when I explicitly put the condition: if (myvar == true) then it works as expected. –  user1608147 Aug 18 '12 at 0:41
First, stop learning from w3schools, as their information is often misguided. So bad that some great front end developers made w3fools.com. developer.mozilla.org/en-US is much better. –  timrwood Aug 18 '12 at 0:43
Thanks for the suggestion! I will check it out. –  user1608147 Aug 18 '12 at 0:46

3 Answers 3

up vote 0 down vote accepted

You're creating a new object with new Boolean(); and objects in JavaScript evaluate to true.

I think if you drop the new keyword and use Boolean() simply as a function, you'll see behavior more in line with what you expect.

share|improve this answer
Check out a good write up on this here: james.padolsey.com/javascript/truthy-falsey. Also, you never need to use Boolean(). Just use true or false. –  timrwood Aug 18 '12 at 0:49
Thank you! That article had my exact problem and explained it well. –  user1608147 Aug 18 '12 at 0:54

the code if (myvar) is wrong, it simple checks if myvar is set. you have to change the statement

if (myvar.toString() == true)
share|improve this answer
Yes, if I explicitly check if myvar == true this works as well. I now know that objects in JavaScript evaluate to true which was the real problem. Thanks. –  user1608147 Aug 18 '12 at 0:59

First of all, jQuery

This could be rewritten like this:

// Perform logic when document is set and ready.
$(document).ready(function() { 
  var myvar = false; // No need for Boolean objects.
  $("demo").html(myvar.toString()); // Set the value of demo.
  myvar ? $("demo1").html("ON") : $("demo1").html("OFF"); // True/false ON/OFF in demo1

Also, JavaScript is nasty for equality testing, have a look here, and if you get the chance, read this.

share|improve this answer
I really like the line: myvar ? $("demo1").html("ON") : $("demo1").html("OFF"); Really cuts down on my if..else statements to toggle. Thanks! –  user1608147 Aug 18 '12 at 1:03

Your Answer


