Why does the first fuunction of let output true and the second one false?

Viewed 22

Why does maxNum and textMax be true when the code is ran and testMin and minNum become false? I am pretty confused about this. Does this have to do with the let function in js?

let maxNum = Number.MAX_VALUE; 
let testMax = maxNum + 10; 
console.log(maxNum, testMax, maxNum === testMax); 
 
let minNum = Number.MIN_VALUE; 
let testMin = minNum / 10; 
console.log(minNum, testMin, minNum === testMin); 

1 Answers

Number.MAX_VALUE is the maximum non-infinity, non-BigInt number that can be represented in the engine. If you try to add a value to Number.MAX_VALUE above the last digit of precision (such as 1e302), you'll get Infinity. (If you try to add a value below the last digit of precision, it'll just be dropped, and you'll still be left with Number.MAX_VALUE)

Number.MIN_VALUE is somewhat similar - it's the smallest positive non-zero number that can be represented. It's not possible to go any lower without reaching 0. If you divide Number.MAX_VALUE by 10, you'll get a number too close to 0 to be represented as anything else, so the engine gives you 0 in response.

Related