0

I am trying to confirm for myself whether a null object is false. First, I tried to create a null object using Object.create(null). Then I decided to test this object against null.

I wrote a snippet of code to check for truthiness:

let emptyObject = {};
let nullObject = Object.create(null);
console.log(typeof nullObject);

console.log(Boolean(null));
console.log(Boolean(nullObject));
console.log(Boolean(emptyObject));

But my output is the following:

object
false
true
true

The nullObject is not empty nor undefined, but why does it not have the same truthy value as null? Are these not both values of null with object type, object?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    Does this answer your question? https://stackoverflow.com/questions/21743921/javascript-is-an-empty-object-a-falsy-one – codesnerd Feb 22 '21 at 03:27
  • `Object.create(null)` still gives you an object, not an actual `null`. The only difference is that the prototype is not set but type and equality don't rely on that anyway. – VLAZ Feb 22 '21 at 14:16
  • Does this answer your question? [Javascript: Is an empty object a falsy one?](https://stackoverflow.com/questions/21743921/javascript-is-an-empty-object-a-falsy-one) – VLAZ Feb 22 '21 at 14:18
  • 1
    Also relevant: [Creating JS object with Object.create(null)?](https://stackoverflow.com/q/15518328) – VLAZ Feb 22 '21 at 14:19
  • I didn't know a null object was still considered empty. I tried to equate emptyObject == nullObject. I know that an emptyObject is truthy. – Codenameskidsnextdoor Feb 23 '21 at 04:17

0 Answers0