DEV Community

Uncovering Hidden Gems in JavaScript

Subham Behera on July 05, 2024

JavaScript is one of the most versatile and widely used programming languages in the world. It powers everything from simple websites to complex we...
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

The !! Double Bang Operator

There is no such operator, you are simply using the logical not (!) operator twice.

Collapse
 
wintersboy profile image
Adam Winters

I would have highly suggested googling the double bang operator, before you responded saying there is no such thing...

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

Could you point out to me where in the list of unary operators in the official language specification it appears?

tc39.es/ecma262/#sec-unary-operators

Thread Thread
 
wintersboy profile image
Adam Winters

Ahh okay I misread what you said, sorry. It's technically not its own operator, just a combination of 2 logical nots.

However, it is widely known as the double bang operator so I wouldn't go as far as saying "there's no such operator".

Thread Thread
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

I would, as it creates a misunderstanding of the language and what's going on

Thread Thread
 
wintersboy profile image
Adam Winters

I mean, the += operator isn't in the list of unary operators, but it's still widely used and known as a JavaScript operator.

He also never stated that !! was a unary operator.

Thread Thread
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

The += operator isn't in the list of unary operators because it isn't one. It is an assignment operator. Assignment operators take two operands.

The very fact that he talked about it as a single operator and shows it used in the a manner it is - implies that he thinks it is a unary operator.

Thread Thread
 
nibelune profile image
Benoรฎt Schiex

While Jon is technically right... the double bang name is widely used to name the trick of using 2 single bang operators to convert a value to boolean.

Documentations are not law books. take a seat and have a beer :)

Thread Thread
 
chovy profile image
chovy

Boolean(user) is how that should be done.

Thread Thread
 
samuel-braun profile image
Samuel Braun • Edited

Yup Boolean(user) it should be but something like user != null would be even better. Don't try and be smart with funky syntax. You could also use ~~number to floor it (5.123 becomes 5), but jokes on you when you run into negative numbers and get an off by one error ๐Ÿ™ƒ

Collapse
 
deepak_vishwakarma_b61261 profile image
Deepak Vishwakarma

@jonrandy Such cool post