Home > front end >  A use case of null's intentional absence in defining nodejs object
A use case of null's intentional absence in defining nodejs object

Time:11-15

Here is the definition of null in nodejs:

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

intentional absence is my use case. Here is the code:

let sqlstring = {
  uploader_id:_uploader_id,  //here _uploader_id is null
  status:_status,  //_status is null
}

Based on the definition of null, when both _uploader_id and _status have null, then sqlstring shall be {} because null means the value is intentional absence. However the console output has this:

sqlstring : {
  uploader_id:null,
  status:null
}

What I am expecting is :

 sqlstring : {}

What is missing here?

CodePudding user response:

An object can have a member with null value, and this looks different from an object where this member is absent. I find the first sentence of the Node.js definition misleading, the second sentence is clearer: null is a primitive value, like 0 or 1.

If you write a function that compares two objects, you are free to treat null like absence, so that

objectsAreEqual({uploader_id: null, status: null}, {})

returns true.

  • Related