Home > database >  Are null and Infinity interchangeable in Javascript?
Are null and Infinity interchangeable in Javascript?

Time:11-02

I'm setting max to Infinity on this object:

let RANGE_DEFAULT_OPTIONS: any = { min: 0, max: Infinity };
console.log(RANGE_DEFAULT_OPTIONS); // {min: 0, max: null}

When the RANGE_DEFAULT_OPTIONS object is logged, it logs null for the max property.

enter image description here

const obj = { min: 0, max: Infinity };

console.log(obj);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

In the case of StackBlitz, there’s probably a call to JSON.stringify(…) somewhere down there, which messes things up (which is sad, IMO):

enter image description here

CodePudding user response:

Here's the same code in a snippet you can run in your browser. As you can see, Infinity and null are not equal.

let RANGE_DEFAULT_OPTIONS = { min: 0, max: Infinity };
console.log(RANGE_DEFAULT_OPTIONS);
console.log('strict equality', Infinity === null);
console.log('loose equality', Infinity == null);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related