You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in JavaScript every Number is a Float -> unlike other Languages, JS does NOT know special Integer Types
Numbers are stored as 64 Bit Floating Points (-> binary system)
// biggest integer (i.e. no decimal places required)Number.MAX_SAFE_INTEGER;// 9007199254740991Math.pow(2,53)-1;// 9007199254740991Number.MIN_SAFE_INTEGER;// -9007199254740991// largest decimal numberNumber.MAX_VALUE;// 1.7976931348623157e+308
Floating Point Imprecision
0.2+0.4;// 0.60000000000000010.2+0.4===0.6;// false -> since JS works internally in binary system, NOT in decimal system, so converts your decimal into binary, does calculation and re-converts into decimal for humans// [1] workaround to fix imprecision(0.2+0.4).toFixed(1);// 0.6(0.2+0.4).toFixed(1)===0.6.toFixed(1);// true// [2] workaround to avoid imprecision: multiply decimals by 1000.2*100+0.4*100===0.6*100// true
BigInt Type
add n at the end of number
number is then treated as a string
notice: no floating point allowed
90071992547409911239123n10n-4n;// 6n10n-4;// TypeError: can NOT mix it5n/2n;// 2n -> decimal is cutted
Math.PI;// approximately 3.14159Math.random();// returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1Math.floor();// returns largest integer less than or equal to x.constrandomIntBetween=(min,max)=>{// +1 needed, without it could never yield minimum and maximum since 0 and 1 are not included into random()// floor() cuts decimals up that max return would be our max parameterreturnMath.floor(Math.random()*(max-min+1)+min);}