0

Recently - learning and working with angularjs api. Looked at the source code for angular.isElement(). I understand foundationally what "!!" does to the return value of the expression when using it, but not understanding why use it - is it not redundant? Searched for !! not much info. Link to api page.

function isElement(node) {
  return !!(node &&
    (node.nodeName  // we are a direct element
      || (node.prop && node.attr && node.find)));  // we have an on and find method part of     jQuery API
}

Update: regarding being marked as a duplicate — Thx. Weird that i did not see that question on original search.

Though many long answers- reading them - they don't really answer where I am confused. Is it not redundant in this case? Wouldn't it return true without !!. thx

jamie
  • 690
  • 7
  • 18
  • 1
    _"is it not redundant?"_ - If you need the function to return an actual boolean value and not just a truthy or falsy value then it's not redundant. – nnnnnn Dec 14 '14 at 02:18
  • Personally I believe the angularjs tag should be removed. This is a javascript syntax question, not an angularjs question. – David L Dec 14 '14 at 02:18
  • 1
    `&&` in JS does not result in a boolean. – loganfsmyth Dec 14 '14 at 02:18
  • ``!!` converts anything to a boolean. – Derek 朕會功夫 Dec 14 '14 at 02:20
  • 2
    I don't think this is a duplicate. The other question is more "what does it do?", while this is "why would you do it, isn't it redundant?" – nnnnnn Dec 14 '14 at 02:22
  • If this isn't a duplicate of the various `!!` questions, then it's a duplicate of questions regarding JavaScript's "guard" (`&&`) and "default" (`||`) operators and how they work. – JAAulde Dec 14 '14 at 03:10
  • found this after reading answer from Stuart. helped as well with 'truthy' vs true logic. https://gist.github.com/jfarmer/2647362. thx:) – jamie Dec 14 '14 at 04:08

4 Answers4

2

!! is used to convert a "truthy" or "falsey" value into true or false, and is equivalent to Boolean(). For example

!!0              // false
!!1              // true
function Obj() {}
o = new Obj()
!!o              // true
!!''             // false
!!'hi'           // true
(1 && 3 && 4)    // 4
!!(1 && 3 && 4)  // true

!!x can be read out loud as 'not not x'. The first ! turns a truthy value to false and a falsey value to true. Then the second ! reverses this so that the truthy value becomes true and the falsey value becomes false.

As to why people use that rather than Boolean, this appears to be just a convention. It is concise and easily understood by most programmers, and there is possibly some resemblance to how people do things in C or C++. It is not particularly faster.

EDIT. You want to know why it is not redundant in this particular case. We may have no idea what type of object is being passed to the function as node. && in javascript works from left to right and if all of the operands are truthy, returns the right-most operand, rather than true or false. Try redefining isElement without the !!. We get the following responses:

isElement('hi')                      // undefined
isElement(0)                         // 0
isElement(3)                         // undefined
isElement({nodeName: 'something'})   // 'something'
isElement({prop: 100, attr: this, find: Infinity})   // Infinity

These results will in fact be handled well most of the time -- in any if statement the truthy values ('something', Infinity) will be treated as true and the falsey values (undefined, 0) treated as false. But still, the user of the API will expect it to return only true or false and there will occasionally be unexpected behaviours if it is allowed to return any of these things.

Community
  • 1
  • 1
Stuart
  • 9,597
  • 1
  • 21
  • 30
1

!!var === Boolean(var). This was not always true, but it is for modern interpreters. In the old interpreters, Boolean(var) always returned true, because it is an object, so people used !! to get the equivalent value.

...and of course it is shorter

twinlakes
  • 9,438
  • 6
  • 31
  • 42
0

It converts the value to a boolean. !! isn't an operator itself. It is just the negation operator ! twice.

Talmid
  • 1,273
  • 14
  • 19
-1

The && operator does not always return a Boolean. It actually returns either false or the second value.

That's why !! is used to force it to be a Boolean, because the APIA is apparently supposed to return one.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • It doesn't return `false` necessarily - it returns the first falsey value or the last truthy value. – Stuart Dec 14 '14 at 11:17