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?
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?
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.
!!
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++. –
Peebles !!
depends on the code guidelines one has got to obey to. –
Ecospecies ==
vs ===
, implicit ;
, etc.), especially if they consider it "just a browser scripting language". –
Peebles 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.
new
keyword it works fine though. typeof Boolean(1) === "boolean"
. –
Ecospecies Boolean()
, not new Boolean()
. Those are two different things. Also, new
is an operator, not a constructor. –
Barde new
isn't used. Thanks! –
Foxtrot 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.
It might just be a case of using less characters, making for a more compact script.
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"
Boolean(...)
is actually the question if I'm not wrong. That also returns correct typeof
results. –
Ecospecies 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. –
Milissa 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.
© 2022 - 2024 — McMap. All rights reserved.
!!
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. – Kella!!
is perfectly readable in my opinion. I prefer those shortcuts. – Ecospeciesx=!!0;
for example. Then look at this:x = new Boolean(0);
. You really think!!0
is more readable? – Kella!!someVariable
is really compact and nice. – EcospeciesBoolean(0)
, notnew Boolean(0)
. – Bardevar x = 0; !!x
? You're changing the semantics. Sorry, you're wrong. I bet you think Perl is a beautiful language. – Kella!!
is and at least half wouldn't know. My point being:Boolean(x)
more clearly conveys what is trying to be achieved than!!x
. – Peebles!!
and such are also available in other languages like PHP, btw. – Cinquefoil