-3

I don't understand this line as well

data: !!data && _.mapObject(data, ... 

What is the signification of "!!" In the following JS code?

$.ajax({
  type: "POST",
  url: url,
  dataType: "json",
  async: true,
  data:
    !!data &&
    _.mapObject(data, function(value) {
      return $.toJSON(value);
    }),
  statusCode: {
    500: function() {},
  },
  success: function(response) {
    if (_.isUndefined(response.isDocument)) {
      response.isDocument = that._currentSearch.type == "document";
    }

    if (response && response.searchResult) {
      $(response.searchResult.hits).each(function(i, item) {
        item.uniqueHitId = SearchModel.generateUniqueHitId();
        searchModel._cacheResult[item.uniqueHitId] = item;
      });

      successHandler(response, that.isFacetListFound, adaptHighlights, filters);
    } else {
      errorHandler("No result found", response.Error);
    }
  },
  error: errorHandler,
});

Normally, !! casts an object to a true value if this object is defined, but in my case, the content of data after this line is :

{
message: "data is not defined"
stack: "ReferenceError: data is not defined"}

Please see this screenshot from a debug result of the execution: enter image description here

ESSABRI
  • 41
  • 2
  • 12
  • `!!` is `not not` it's a common way to make something truthy. – Liam Jan 09 '19 at 14:45
  • https://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript - it's probably a more relevant dupe, as it covers both questions – VLAZ Jan 09 '19 at 14:46
  • 3
    @Liam is more a common way of making something truthy _true_ – robertklep Jan 09 '19 at 14:47
  • What do you mean with not not? Can you let me understand more? thank you – ESSABRI Jan 09 '19 at 14:48
  • Read the [duplicate](https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – Liam Jan 09 '19 at 14:49
  • @liam, I know really what && does mean, but in this context i'm confused! – ESSABRI Jan 09 '19 at 14:49
  • https://stackoverflow.com/questions/50795708/can-someone-explain-this-operator/50795724#50795724 – Karim Jan 09 '19 at 14:49
  • Thank @Karim for help, I understand that the value of data after this line will be a boolean value, but in my case,the result is an object, please see the updated description – ESSABRI Jan 09 '19 at 15:03
  • An object is [coerced into a boolean when you do `!!` on it.](https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript/19915864) – Liam Jan 09 '19 at 15:38
  • Thank @Liam and everyone responded me, Now I understand. – ESSABRI May 31 '19 at 16:34

1 Answers1

0

!! AKA doble not operator is used to make evidence that the value is a boolean or it will be casted as a boolean

One of the benefits of using the !! is to convey that your design decision was to only return a true or false value. When you or someone else looks at that code and a !! is encountered, the only possible return values are true or false. It is a matter of contention if readability is improved with the use of this logical tool.

The most common criticism of using !! is its seemingly added complexity that can be simplified with slightly more code that may have more flexibility. Another criticism relates to zero being a falsy value, so a lot of use cases related to numbers come with extra concerns.

Ref: https://medium.com/@edplatomail/js-double-bang-or-the-not-operator-part-40e55d089bf0

The && operator evaluate the argument precedes it (!!data) as a boolean and if it is true it will return the argument following the operator (_.mapObject(data, ...), otherwise it will return just false

Mosè Raguzzini
  • 12,776
  • 26
  • 36