Math — вбудований об'єкт з математичними константами та функціями:
Математичні константи:
1Math.PI class="hl-comment">// 3.141592653589793
2Math.E class="hl-comment">// 2.718281828459045Округлення:
1Math.round(4.6) class="hl-comment">// 5
2Math.floor(4.9) class="hl-comment">// 4
3Math.ceil(4.1) class="hl-comment">// 5
4Math.trunc(4.9) class="hl-comment">// 4 (відкидає дробову частину)Числа:
1Math.abs(-5) class="hl-comment">// 5
2Math.max(3,7,2) class="hl-comment">// 7
3Math.min(3,7,2) class="hl-comment">// 2
4Math.pow(2, 10) class="hl-comment">// 1024
5Math.sqrt(144) class="hl-comment">// 12
6Math.cbrt(27) class="hl-comment">// 3
7Math.log(Math.E) class="hl-comment">// 1
8Math.log2(8) class="hl-comment">// 3
9Math.log10(100) class="hl-comment">// 2Випадкові числа:
1Math.random() class="hl-comment">// 0 <= x < 1
2Math.floor(Math.random() * 6) + 1 class="hl-comment">// від 1 до 6 (кубик)console.log(Math.round(3.5));
console.log(Math.floor(3.5));
console.log(Math.ceil(3.5));
console.log(Math.trunc(3.5));
const r = 5;
const area = (Math.PI * r ** 2).toFixed(2);
console.log(area);
console.log(Math.sqrt(3**2 + 4**2));
console.log(Math.max(15, 8, 23, 4));
function celsiusToAll(c) {
console.log(`Celsius: ${c}`);
console.log(`Fahrenheit: ${c * 9/5 + 32}`);
console.log(`Kelvin: ${c + 273.15}`);
}
celsiusToAll(100);