DEV Community

Cover image for NOT NOT, Not Working As Expected
bob.ts
bob.ts

Posted on

2

NOT NOT, Not Working As Expected

First, NOT NOT ...

The single ! converts a value to its truthy or falsey value, which is technically a boolean. But if you need to a real boolean representation of a value for your expression you must convert it to a real boolean value using a double not, !!.

In my head, I could see the conversion. I hear myself evaluating it as "does this object exist." Knowing that was wrong, I still dug into the code to find out why things were breaking in other areas.

Here's a simple example of the faulty (logic) code.

const data = { params: { type: '' } };

if (!!data.params && !!data.params.type) {
  // do something here
}
Enter fullscreen mode Exit fullscreen mode

This code refused to go inside the IF-BLOCK.

After digging into the console, I realized ...

!!data.params
// true

!!data.params.type
// false
Enter fullscreen mode Exit fullscreen mode

What I quickly realized is that I got bit by a simple logic issue. An empty string equates to false, while a string with something in it equates to true.

A better set of logic would have been to use the IN operator.

const data = { params: { type: '' } };

if (('params' in data) && ('type' in data.params)) {
  // do something here
}
Enter fullscreen mode Exit fullscreen mode

Then, the inner code for the IF-BLOCK would have worked properly.

Another method that can be used is the hasOwnProperty method ...

const data = { params: { type: '' } };

if (data.hasOwnProperty('params') && data.params.hasOwnProperty('type')) {
  // do something here
}
Enter fullscreen mode Exit fullscreen mode

Generally, I prefer the first of the two solutions. To me, this seems more readable, but that's my preference.

Top comments (2)

Collapse
 
kaleman15 profile image
Kevin Alemán

Hey! Just a couple of things:

In your example with if ('params' in data) && ('type' in data.params)) { you're missing a ( from the if statement.

The True and False are more python things, maybe you can use true and false.

Good article! Keep it up!

Collapse
 
rfornal profile image
bob.ts

I'll make the adjustments now. Thanks for catching the missing paren.

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

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay