1

I am a react developer and writing the client on pure JS. I have sometimes missions to received data from server and send data to server. Now between the client team and backend rough discussion regrading how to send the data.

The server use map object for almost every thing, even for small data sets of information, their main reason is performances of the server in terms of time complexity.

Now I had a mission for handling a small data set which was represented as map object on server. I requested to send me a list of objects instead map of objects, after hours of debate, the server accepted my offer. But then all the empty values which were represented as nulls, are now represented as empty object({}).

So I am asking what is better to describe the absent of a field? Null value vs empty object({}) ? what is more popular in our days?

1
  • 2
    Is there any reason for the server-side development to make (so simple) things more complicated than usual? Do they think complexity and time-complexity are exclusive of server-side developments? I get the feeling that your teammates are missing the point of "server" side developments.
    – Laiv
    May 21, 2023 at 8:51

2 Answers 2

2

Empty object ({}) is definitely not the right way to represent a missing value! An empty object could be a valid object where all properties are optional, so using this to represent a missing object would introduce ambiguity.

JavaScript supports both undefined and null for missing values, but in the context of an array, null is the only reasonable option, since JSON cannot represent undefined array elements.

(For objects, an undefined property can be represented in JSON by just excluding the property. But this is not possible with arrays. While [foo, , bar] is valid JavaScript, it is not valid JSON.)

2
  • "but in the context of an array ... since JSON cannot represent undefined array elements" - This statement sounds a bit misleading, because one may think that for non-array types also undefined is supported. But this is not true. JSON specification does not have undefined, only null. I'd suggest you to refine the wording in this part. 🙂
    – mentallurg
    May 20, 2023 at 18:39
  • good answer, thank you both :)
    – Brk
    May 20, 2023 at 18:42
2

See how specification ECMA, 4.4.15 null value defines null value:

primitive value that represents the intentional absence of any object value

Empty object is an object. null means that a value or an object is not defined. Where as empty object is an object, it is not null. It is similar to an empty string "" vs null string.

If some object is not defined, then is should not be present at all or should be set to null, which means the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.