Note Number() is stricter -- rejects partially numeric strings. parseInt() stops at the first non-numeric character. Always pass the radix to parseInt() to avoid octal surprises.
const total =29.5;console.log(total.toFixed(2));// "29.50"console.log((0.1+0.2).toFixed(2));// "0.30"const big =123456.789;console.log(big.toPrecision(6));// "123457"
Note toFixed() returns a STRING, not a number. Wrap in Number() or use + to convert back: +total.toFixed(2)
// Random float between 0 and 1console.log(Math.random());// Random integer from min to max (inclusive)functionrandomInt(min, max){returnMath.floor(Math.random()*(max - min +1))+ min;}console.log(randomInt(1,100));
Note Math.random() is NOT cryptographically secure. For tokens or passwords, use crypto.getRandomValues() or crypto.randomUUID().
random numbergenerate randomrandom integerMath.random
Min, Max, and Clamping
Syntax
Math.min(...values)Math.max(...values)
Example
const scores =[88,45,99,72,61];console.log(Math.min(...scores));// 45console.log(Math.max(...scores));// 99// Clamp a value between boundsfunctionclamp(value, min, max){returnMath.min(Math.max(value, min), max);}console.log(clamp(150,0,100));// 100
Note For very large arrays (100k+ elements), spread can cause a stack overflow. Use a reduce loop instead.
min valuemax valuefind minimumfind maximumclamp number