最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does this expression mean "!!" - Stack Overflow

programmeradmin3浏览0评论

Why would somebody use an expression like

if(!!window.JSON) alert('exists');

instead of just

if(window.JSON) alert('exists');

?

Is there another usage for the "!!" thing ?

Why would somebody use an expression like

if(!!window.JSON) alert('exists');

instead of just

if(window.JSON) alert('exists');

?

Is there another usage for the "!!" thing ?

Share Improve this question edited Jun 17, 2010 at 7:16 Alex asked Jun 17, 2010 at 7:06 AlexAlex 512 bronze badges 3
  • Multiple negatives are though to understand, that's why. If you keep using if(!!(!!)!!!(!)!!obj) in your code, only you can maintain it (if your lucky) - I'm not no undummy! – simendsjo Commented Jun 17, 2010 at 7:10
  • 1 see stackoverflow./questions/1406604/… ; and you're right, in this case it's redundant, as if() already converts to boolean – Christoph Commented Jun 17, 2010 at 7:19
  • This question is similar to: What does the !! operator do in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Donald Duck Commented Nov 7, 2024 at 14:21
Add a ment  | 

5 Answers 5

Reset to default 15

!! will simply cause a boolean conversion, it is not necessary at all to use this construct with the if statement, also in your first snippet, converting an object to boolean makes no sense, it will be always true.

The only values that can coerce to false are null, undefined, NaN, 0, and empty string and of course false anything else will coerce to true.

When it makes sense to use !! is when you want to make sure to get a boolean result, for example when using the Logical && and || operators.

Those operators can return the value of an operand, and not a boolean result, for example:

true && "foo"; // "foo"
"foo" || "bar" ; // "foo"

Imagine you have a function that test two values, and you expect a boolean result:

function isFoo () {
  return 0 && true;
}

isFoo(); // returns 0, here it makes sense to use !!

Edit: Looking at your edit, if (!!window.JSON) is just redundant as I said before, because the if statement by itself will make the boolean conversion internally when the condition expression is evaluated.

They're probably trying to cast the object to a boolean. By applying a boolean operator (!) to the object, it bees a bool, but they don't actually want the negation, so they apply a second one to cancel the first.

// !! = boolean typecasting in javascript

var one = 1
var two = 2;

if (one==true) app.alert("one==true");
if (!!one==true) app.alert("!!one==true");

if (two==true) app.alert("two==true"); // won't produce any output
if (!!two==true) app.alert("!!two==true");

! is a logical negation, !! is the logical negation applied twice, ie the logical identity

I would assume the code you are reading is just being harmlessly (albeit confusingly) redundant.

Its a boolean conversion, ensuring a conversion to boolean the first time then negating back to the original value.

However it is used incorrectly here and would only really be relevant if you assign it to another variable - for performance reasons

var isJSON = (!!window.JSON);

if (isJSON) doX;
if (isJSON) doY;
if (isJSON) doZ;
发布评论

评论列表(0)

  1. 暂无评论