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?

12
  • 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. Commented Mar 14, 2011 at 18:31
  • 9
    !! is perfectly readable in my opinion. I prefer those shortcuts. Commented Mar 14, 2011 at 18:32
  • 2
    No, both methods produce the same result. Commented Mar 14, 2011 at 18:32
  • 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. Commented Mar 14, 2011 at 18:35
  • 2
    The !! and such are also available in other languages like PHP, btw. Commented Mar 14, 2011 at 18:59

7 Answers 7

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.

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

6 Comments

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, 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++.
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.
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 ...
@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".
|
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.

3 Comments

Without the new keyword it works fine though. typeof Boolean(1) === "boolean".
The OP was referring to Boolean(), not new Boolean(). Those are two different things. Also, new is an operator, not a constructor.
@Sime Vidas fixed, and added a clarification that it doesn't apply for cases where new isn't used. Thanks!
2

I can think of 7:

  1. B
  2. o
  3. o
  4. l
  5. e
  6. a
  7. n

Comments

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.

Comments

1

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

Comments

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"

2 Comments

Just using Boolean(...) is actually the question if I'm not wrong. That also returns correct typeof results.
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.
0

The Boolean(...) syntax is probably the best option, especially if you’re working on a large application with multiple developers. Code should be as readable as possible for any developer who joins the project.

Just as you should avoid using acronyms in your communication to ensure clarity for a wider audience, your code should also be easily understandable for most developers without needing additional research.

This time, we’re discussing the !! syntax, but there are many strange syntaxes in JavaScript. Reading such code for the first time can be challenging if efforts aren’t made to ensure readability. Moreover, even for experienced developers, it’s easier to work with clear and readable code.

This debate makes sense at the scale of !!, but at the level of an entire codebase, there’s no debate for me, so !! should be avoided.

Comments

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.