|
|
|
|
|
by Stratoscope
3743 days ago
|
|
One typical use for !! in JavaScript or Ruby is where a function/method returns a boolean value that indicates the presence or absence of an object. You could just use: return myObject;
Since that value will be truthy or falsy, any code that tests it will generally work, as your if statement example demonstrates.But there are two advantages to doing this instead: return !! myObject;
1) Now you are returning an actual boolean value, not just a truthy/falsy value.2) Sometimes more importantly, you are now releasing this function's reference to the object, instead of possibly keeping that reference around much longer than needed and preventing it from being garbage collected. |
|