admin管理员组

文章数量:1288075

In this code sample from the sequlize docs:

if (!!err) {
    console.log('Unable to connect to the database:', err)
} else {
    console.log('Connection has been established successfully.')
}

Why are they using (!!err) to test that err's truthiness? Isn't that the same as if (err)?

In this code sample from the sequlize docs:

if (!!err) {
    console.log('Unable to connect to the database:', err)
} else {
    console.log('Connection has been established successfully.')
}

Why are they using (!!err) to test that err's truthiness? Isn't that the same as if (err)?

Share Improve this question edited Dec 3, 2014 at 12:34 Salman Arshad 272k84 gold badges442 silver badges534 bronze badges asked Dec 2, 2014 at 19:55 ericeric 2,7594 gold badges31 silver badges44 bronze badges 6
  • 2 As far as I know it is the same... – brso05 Commented Dec 2, 2014 at 19:56
  • 2 Yes, it's the same for checking truthiness. I imagine it's just people being pedantic about testing an actual boolean in the conditional expression over letting the interpreter coerce it for them. IMHO, unless you want to explicitly return a boolean (e.g. return !!err;), a !! is superfluous. – ajp15243 Commented Dec 2, 2014 at 20:01
  • 1 Not quite the same ... err is an object. !! converts it to an inverted boolean, then inverts the boolean again ... If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true. So !! is not an operator, it's just the ! operator twice. – rfornal Commented Dec 2, 2014 at 20:02
  • 1 It does not serve any purpose inside the if statement, anything truthy triggers the if block. – Salman Arshad Commented Dec 2, 2014 at 20:04
  • 4 This question is NOT a duplicate of stackoverflow./questions/784929/…. That question provides info about the !! syntax and what it does, but does not explain why it is used in the OP's question. That is a further question and it turns out the answer is that there is no need for that syntax in the OP's code example since if (err) and if (!!err) generate the same logic either way. – jfriend00 Commented Dec 2, 2014 at 20:09
 |  Show 1 more ment

3 Answers 3

Reset to default 13

Why are they using (!!err) to test that err's truthiness?

There's no reason. Maybe they're overcautious, having heard some wrong things about thruthiness? Or they want to emphasize the ToBoolean cast that occurs in the evaluation of the if condition?

Isn't that the same as if (err)?

Yes.

if (err)
if (Boolean(err))
if (!! err)

all mean exactly the same thing. The latter two only doing unnecessary steps in between before arriving at the same result.

Double exclamation mark convert any thing to the Boolean representation of that expression whether it is true or false in sense of Boolean value

OR

It converts a nonboolean to an inverted boolean

for example define a string

var vari = "something";

and check for its boolean equivalent

console.log(!!vari);

Why part of you Question

Author or writer may be over Precocious to check of the existence of the error. Or have an extra check that some one is not passing an expression instead of error that need to be checked as boolean. In Both ways it is doing same thing So i explained what it means you dont need to worry about why now as they are same authors intent can be

!! intent is usually to convey to the reader that the code does not care what value is in the variable, but what it's "truth" value is.

As Bhojendra - C-Link Nepal gave example

err = 'This is also to be a true if you convert it to boolean';
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

one is considered true but 'this sentence' is not equal to 1 , so being true still one if block will be executed in above code

It really matters the same thing.

But, wait! It's really different thing when we use to test true or false:

err = 'This is also to be a true if you convert it to boolean';
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

//outputs only: test by converting to boolean

But a real boolean value logs both as in the following example:

err = true;
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

So double exclamation sign is used to convert to a boolean only.

So, people use the condition to make sure the value is in boolean or not and if it's not then convert it and check it but if he wanted to check strictly that is true or not then he'll pare using 1 === 1 and in the above example exactly the same thing is being applied.

But just like in your code if(!!err){ is used to make sure it is false or true because of lack of deep knowledge that if(err){//string even can be checked as true or false.

That's it. The exactly check is made by !!.

本文标签: javascriptWhy use if (err)Stack Overflow