Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does -Infinity actually work?

Tags:

javascript

    STACKOVERFLOW = [];
    turkeycheck = Math.max.apply(null, STACKOVERFLOW);
    if(!turkeycheck){
         // See Below
    }

console.log(turkeycheck);
console.log(turkeycheck.length);

Can also be visible here: http://jsfiddle.net/L4zgfcmz/5/

Just curious why (!turkey) doesn't get catched but using turkeycheck.length shows it's undefined? If it's undefined doesn't that mean it's false?

Also, if turkey is console logging -Infinity, shouldn't the length of turkeycheck become 11?

I'm just confused as why the turkey is being set to '-Infinity', but then becomes undefined.

like image 318
NiCk Newman Avatar asked Feb 14 '26 18:02

NiCk Newman


1 Answers

-Infinity is a numeric value. Numeric values do not have a .length property, so attempting to read from it will return undefined. The value held in your turkeycheck variable itself remains unchanged, and if you attempt to log its value after attempting to log its length, you'll still be shown the value -Infinity.

var n = 1;
console.log(n.length); // undefined
console.log(n);        // 1
like image 127
James Donnelly Avatar answered Feb 17 '26 07:02

James Donnelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!