Skip to main content

What"s the point of the Boolean Object?



I am reading through the Mozilla Manual on JavaScript and I come to this point in my reading, Boolean Object and I can't see a single use for them. What's their point? Why would'nt you use just true and false ?





By the way, I don't know Java at all and I'm not afraid of learning new things that consequently why I'm trying to learn JavaScript. I'm a PHP programmer, a back end guy, and I'd like to learn how to do some front end work, so I'm reading the Mozilla JavaScript Guide .



Source: Tips4all

Comments

  1. Because this is (somewhat sadly) how the language was defined -- I suspect it was originally for performance/optimization; note the case of assignment to a string property below. (Java works similarly, although Scala and Python largely reject this distinction).

    Note that Boolean isn't the only "wrapper type". There are also String and Number, for instance.

    Because of this there remains a number of quirks (the below could just as much apply to Boolean):

    typeof("foo") // string
    typeof(new String("foo")) // object
    "foo" instanceof String // false
    new String("foo") instanceof String // true

    // result is undefined: a string is a primitive and silently "ate" the assignment
    // this also makes it a much cheaper value as it's not a "real" object
    x = "f"; x.bar = 42; x.bar

    // result is 42: a String is a "real" object with real properties!
    // however, this also means that it may have a good bit more overhead
    x = new String("f"); x.bar = 42; x.bar


    I know this didn't "answer" the question, but rather chucks some more wood on the fire ;-)

    The only real "gotcha" otherwise from the above is that perhaps new Boolean(false) is a truth-y value.

    Happy coding.

    ReplyDelete
  2. JavaScript language design has quite many dusty corners, the Boolean is one of them.
    Not used in practice.

    This:

    var a = [];
    alert( a instanceof Array );


    will tell you "true". But this:

    var b = true;
    alert( b instanceof Boolean );


    for some reason will show "false".

    To be short: forget about it.

    ReplyDelete
  3. Creating a new boolean object "basically" runs the bit of code in the statement and then from there returns the true boolean value.

    From the same docs:

    1 var b = new Boolean(false);
    2 if (b) // this condition evaluates to true


    https://developer.mozilla.org/en/JavaScript/Guide/Statements#if...else_Statement

    ReplyDelete
  4. Perhaps because Javascript Objects are extensible in ways that primitives aren't? (I'm just guessing here, I've never had a need for Boolean.)

    ReplyDelete
  5. From the documentation:


    Do not confuse the primitive Boolean
    values true and false with the true
    and false values of the Boolean
    object. Any object whose value is not
    undefined , null, 0, NaN, or the empty
    string , including a Boolean object
    whose value is false, evaluates to
    true when passed to a conditional
    statement.


    Imagine the following scenario:

    if(SomeBoolean){...}


    will be true in scenarios where SomeBoolean is a Boolean object.

    Contrapositively:

    if(false){...}


    will always be false

    Addendum for clarification.

    var someString = new Boolean("MyNonEmptyString")
    if(someString) //true
    var otherString = new Boolean("")
    if(otherString) //false

    ReplyDelete
  6. You can coerce a true or false from any value with return Boolean(something),

    but its shorter to write return !!something, which forces a true or false as well.

    ReplyDelete
  7. Boolean.prototype.bang = function() {
    return !this.valueOf();
    }

    true.bang(); // false


    Everything in JavaScript is an object. But at the same time we also have primitives. It's really confusing, just don't overthink it.

    ReplyDelete
  8. Going back to the spec (ECMA-262.pdf page 151), note that when Boolean is called as a function rather than as a constructor, it does type conversion. Thus:

    var t = 5
    , f = 0

    console.log(Boolean(t)) //prints true
    console.log(Boolean(f)) //prints false


    Of course, being a JS object, you can use it as a prototype using the 'new' operator, as others have noted, but I don't see any reason for doing that.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.