1

I was following a tutorial online and they set the predefined value of the function as null(data and details), what is the use of the null, what does it mean

onClick={(data, details = null) => {
        console.log(data.description, details);
      }}
Adokiye Iruene
  • 740
  • 2
  • 10
  • 34

2 Answers2

2

I think you know what is the notation of putting null in parameters and Set a default parameter value for a JavaScript function explains it further.

There are usages of putting default value to null.

  1. You can easily check whether values are assigned with lesser code. ex: if(details){}
  2. You can directly send that to database as a null value.
  3. You may do not prefer default undefined value based on your application requirements

Hope this helps.

Arosha
  • 1,311
  • 2
  • 16
  • 22
1

null is a false-evaluated ("falsey") JavaScript object type whereas an empty object (like one defined via {}) is a true-evaluated ("truthy") object.

In many situations I have seen, developers will use it to signify the parameter is expecting an object because typeof null === "object". Without the details = null, a missing details parameter would be undefined, a type all of its own (typeof undefined === "undefined").

Steve Hynding
  • 1,799
  • 1
  • 12
  • 22