pragmatic84
pragmatic84

Reputation: 107

jslint expected !! and instead saw?

Now before anyone links this article: JSLint : Expected '!!' and instead saw '?'

I want to explain that I think I understand why jslint is giving me this error, however I am unsure as to how I would rework the line of code below to utilise the !! operator.

active = $item.find('isActive').text() === 'true' ? true : false,

Would someone mind enlightening me on this?

I tried doing this first but even to my novice eyes I could see this was basically nonsense :

active = $item.find('isActive').text() === !! 'true' true : false,

I want to write clean javascript so i'd rather get a proper explanation as to why using !! instead of ? is good practice. Looking on the JSLint errors site didn't shed any light on this sadly.

Thanks in advance

Upvotes: 0

Views: 461

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074038

I am unsure as to how I would rework the line of code below to utilise the !! operator.

You wouldn't in this case, neither !! nor ? : is appropriate here. === evaluates to a boolean, so simply:

active = $item.find('isActive').text() === 'true'

All of the equality operators (===, !==, ==, !=) and relational operators (<, <=, >, >=) result in true booleans.

If you wanted to set active to a boolean value based on whether something was truthy (coerces to true) or falsy (coerces to false) then !! would be appropriate. But not when you already have a boolean.

For instance, suppose you want to set blank based on whether str is falsy. In that case,

blank = !!str

would be reasonable.

Upvotes: 3

Related Questions