5

I am aware of Boolean(), String() and Number() casting, and the '' + ..., !!... and +... casting approaches.

I am wondering if there is any reason to not use the function constructors?

Tower
  • 98,741
  • 129
  • 357
  • 507
  • 3
    `!!` and others are unintentional syntactic sugar. I prefer the constructor approach as it is more readable. If you run it through a compressor/minifier, it can convert it into the uglier yet more compact version. – Travis Webb Mar 14 '11 at 18:31
  • 9
    `!!` is perfectly readable in my opinion. I prefer those shortcuts. – pimvdb Mar 14 '11 at 18:32
  • 2
    No, both methods produce the same result. – Šime Vidas Mar 14 '11 at 18:32
  • @pimvdb Imagine you've just read 500 lines of code and you come to `x=!!0;` for example. Then look at this: `x = new Boolean(0);`. You really think `!!0` is more readable? – Travis Webb Mar 14 '11 at 18:34
  • 1
    @Travis: That's a bad example to be honest. That one looks ugly because that are just four ugly tokens together. Also you just remove the spaces in the first one. I mean, e.g. `!!someVariable` is really compact and nice. – pimvdb Mar 14 '11 at 18:35
  • See [this question](http://stackoverflow.com/questions/856324/what-is-the-purpose-of-new-boolean-in-javascript). – Matt Ball Mar 14 '11 at 18:36
  • 1
    @Travis `Boolean(0)`, not `new Boolean(0)`. – Šime Vidas Mar 14 '11 at 18:36
  • @pimvdb So you propose I should do `var x = 0; !!x` ? You're changing the semantics. Sorry, you're wrong. I bet you think Perl is a beautiful language. – Travis Webb Mar 14 '11 at 18:37
  • @Travis: One-letter variables are perhaps not a good example, but the one I stated is preferable for me. But that's just my opinion :) I guess you like XML with all the typing? – pimvdb Mar 14 '11 at 18:38
  • There's a difference between which is more compact and which better conveys the intentions of the programmer. I bet you could ask fresh CS grads if they know what the purpose of `!!` is and at least half wouldn't know. My point being: `Boolean(x)` more clearly conveys what is trying to be achieved than `!!x`. – Andrew Marshall Mar 14 '11 at 18:44
  • 1
    To be fair Andrew, most fresh CS grads wouldn't know what a prototypical language is either. – JaredMcAteer Mar 14 '11 at 18:49
  • 2
    The `!!` and such are also available in other languages like PHP, btw. – Tower Mar 14 '11 at 18:59

6 Answers6

4

In general the use of !! is often discouraged, as it's not clear to those who haven't seen it before what the actual purpose of it is. That said, it is less than a third the characters of Boolean().

Further, I'm not sure how often you actually need to cast to a boolean in Javascript, as it is often implicitly cast since Javascript is weakly typed.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • 7
    Well to be honest I'm not super-sure I want somebody to mess with my code anyway if they're not familiar enough with JavaScript to know what "!!" means. :-) – Pointy Mar 14 '11 at 18:43
  • @pointy, I think there's probably plenty of good programmers who just haven't been exposed to `!!` and wouldn't know what it's trying to do without either stopping to think about it or looking it up. I don't think I've ever seen `!!` used in JS, most of the time it's in C/C++. – Andrew Marshall Mar 14 '11 at 18:48
  • 1
    Personally, as a hobby programmer I've come it across quite a many times. I understand your point, though. I guess whether or not to use `!!` depends on the code guidelines one has got to obey to. – pimvdb Mar 14 '11 at 18:51
  • I'm not 100% serious about that, but I do think that even great programmers unfamiliar with JavaScript should really study it, because there are a **lot** of landmines - particularly for long-time Java or C# programmers who've grown to see the world that way ... – Pointy Mar 14 '11 at 18:54
  • @primvdb: Definitely. @Pointy I agree, there's definitely quite a bit about Javascript that trips up a lot of those unfamiliar with the language (`==` vs `===`, implicit `;`, etc.), especially if they consider it "just a browser scripting language". – Andrew Marshall Mar 14 '11 at 19:17
  • I agree with @Pointy. Further, it is possible for the Boolean constructor to be modified while the "!" operator is immutable. Use !!. It is terse and means the same thing across many languages. I agree that for a single-language developer this could give pause; however, that's how one learns. Teaching and coaching less experienced developers is the way to go. – Wil Moore III Oct 26 '12 at 19:48
3

Using the new operator with those function constructors can have unreliable effects on the typeof operator. (Edit: As the comments correctly note, this is only when using new Boolean() instead of Boolean())

For example,

var f = new Boolean(true);
if(typeof(f)==="boolean") {//false, since its an object, not boolean
 ....
}

JavaScript Garden has some great examples.

Yahel
  • 37,023
  • 22
  • 103
  • 153
  • 1
    Without the `new` keyword it works fine though. `typeof Boolean(1) === "boolean"`. – pimvdb Mar 14 '11 at 18:34
  • 3
    The OP was referring to `Boolean()`, not `new Boolean()`. Those are two different things. Also, `new` is an operator, not a constructor. – Šime Vidas Mar 14 '11 at 18:35
  • @Sime Vidas fixed, and added a clarification that it doesn't apply for cases where `new` isn't used. Thanks! – Yahel Mar 14 '11 at 19:58
2

I can think of 7:

  1. B
  2. o
  3. o
  4. l
  5. e
  6. a
  7. n
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
2

This shouldn't be an issue, but someone could replace the Boolean function with their own making the two ways not equivalent. For example:

Boolean = function(x) {
    alert('Evil');
    return !x; // Oops
}

var x = 0;
console.log(!!x); // false
console.log(Boolean(x)); // true

That's mostly a theoretical difference, since you shouldn't be replacing built in constructors, but it is a difference.

There could also be a small performance difference because of the name lookup and function call overhead. I wouldn't worry about either of those though in most cases. Just use whichever version you prefer.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
1

It might just be a case of using less characters, making for a more compact script.

kim3er
  • 6,306
  • 4
  • 41
  • 69
0

I find using new with these types can lead to easy confusion or tricky bugs:

var x = new Boolean(true);
console.log( x ); // true
console.log( typeof x ); // "object"

var y = new Boolean('false');
console.log( y ); // true
console.log( typeof y ); // "object"

var z = false;
console.log( z ); // false
console.log( typeof z ); // "boolean"
Ken Redler
  • 23,863
  • 8
  • 57
  • 69
  • 1
    Just using `Boolean(...)` is actually the question if I'm not wrong. That also returns correct `typeof` results. – pimvdb Mar 14 '11 at 19:00
  • `typeof ( new Boolean(false) ) === "object"`, whereas `typeof false === "boolean"`. I think the implicit version is therefore less opaque. I suppose it's a matter of preference. – Ken Redler Mar 14 '11 at 19:06