DEV Community

Cover image for ๐Ÿš€Using !! in Javascript
Yuli Petrilli
Yuli Petrilli

Posted on โ€ข Edited on

2

๐Ÿš€Using !! in Javascript

In JavaScript, the "not not" operator, also known as the double negation operator, is denoted by two exclamation marks (!!). It is a unary operator that converts any value to its corresponding boolean value.

How it Works?

Well, the !! operator works by first converting the operand (your value) into a boolean. It then negates this value and negates it again, resulting in a boolean that reflects the truthiness of the original value.

Let's take a look at the following example:

let x = 5;
let y = !!x;
console.log(y); // true
Enter fullscreen mode Exit fullscreen mode

Here, the value of x is first converted to a boolean, and since x is a non-zero number, it is truthy, so the first negation results in false. The second negation then negates this value again, resulting in true.

Practical Uses

Let's take a look at the following example:

let username = "";
if (!!username) {
  console.log("Welcome, " + username);
} else {
  console.log("Please enter a username");
}
Enter fullscreen mode Exit fullscreen mode

Here, the !! operator is used to check if the username variable is truthy or falsy. If it is falsy (in this case, an empty string), the else block is executed, prompting the user to enter a username. If it is truthy, the if block is executed, welcoming the user with their username.

Wanna try it yourself? What would the result of the following expressions:

!![]
!!{}
!!null
!!"foo"
!!-1

Conclusion ๐Ÿ

Unless you have been using JavaScript for a while this will probably look like some advanced magic but a quick way to remember this is that, !! it's just a shorthand way to convert any value to a boolean.

This operator is a powerful and it can be particularly useful when dealing with certain validations, also, it is a definition commonly asked nowadays in interviews so make sure to keep it present (also, using it will make you look cool).

Happy learning and thank you for reading!

If this article was helpful to you, don't forget to hit that โค๏ธ button and support me with a follow, i will keep posting more content about tech, programming, career development and more! :)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

Sorry to tell you this, but Javascript has no !! operator, you're just using the logical NOT (or negation) operator twice.

console.log(!!1)  // true
console.log(! ! 1)  // true
console.log(!            !1)  // true
Enter fullscreen mode Exit fullscreen mode

Also, in your use case the code would work equally well (and would probably be more efficient, and readable) if you removed !! entirely.

nextjs tutorial video

Youtube Tutorial Series ๐Ÿ“บ

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series ๐Ÿ‘€

Watch the Youtube series

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay