Programinja
Published in

Programinja

!! Double Negation in JavaScript | Write better JavaScript

double negation article main image
Source: Programinja

So you are working on a JavaScript project. There is an expression that returns a result in any data type such as string, number, etc.

However, you need the result in boolean.

Double negation !! will convert any result into either true or false. It’s equivalent to Boolean(value).

const isCitizen = !!user.country.resident === 'US'

How Double Negation !! works?

  1. The first negation converts any value to false if it is true and to true if it is false.
  2. The second negation then operates on a normal Boolean value.
  3. Together they convert any truthy value to true and any falsy value to false.

Double Negation is not a JS Syntax

Double negation is neither a special syntax nor a distinctive operator, rather a sequence of two negations syntax.

Any disadvantages?

Any developer having no clue about double negation will definetlity have to figure out it. But, it makes your code concise and cleaner, without having to write extra lines.

Thanks for reading.

Follow me at LinkedIn: https://www.linkedin.com/in/thealiraza

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Raza

Ali Raza

A full-time developer sharing the content to help build quality software. More @ https://www.linkedin.com/in/thealiraza/ https://twitter.com/nowaliraza