Math

Об'єкт – простір імен Math містить статичні властивості та методи математичних сталих та функцій.

Math працює з числами типу Number. Він не підходить для роботи з BigInt.

Опис

На відміну від більшості глобальних об'єктів, Math не є конструктором. Його не можна використовувати з оператором new або викликати об'єкт Atomics як функцію. Всі властивості та методи Math є статичними.

Примітка: Чимало функцій Math мають точність обчислень, залежну від конкретної реалізації.

Це означає, що різні браузери можуть давати різні результати. Навіть один і той самий JavaScript-рушій на різних ОС чи архітектурах процесора може видавати різні результати!

Статичні властивості

Math.E

Число Ейлера, основа натуральних логарифмів. Приблизно дорівнює 2.718.

Math.LN10

Натуральний логарифм числа 10. Приблизно дорівнює 2.303.

Math.LN2

Натуральний логарифм числа 2. Приблизно дорівнює 0.693.

Math.LOG10E

Логарифм числа e за основою 10. Приблизно дорівнює 0.434.

Math.LOG2E

Логарифм числа e за основою 2. Приблизно дорівнює 1.443.

Math.PI

Значення відношення довжини кола до його діаметра, наближено дорівнює 3.14159.

Math.SQRT1_2

Квадратний корінь з ½. Наближено дорівнює 0.707.

Math.SQRT2

Квадратний корінь з 2. Наближено дорівнює 1.414.

Math[@@toStringTag]

Початкове значення властивості @@toStringTag – рядок "Math". Ця властивість використовується в Object.prototype.toString().

Статичні методи

Math.abs()

Повертає модуль числа x.

Math.acos()

Повертає арккосинус числа x.

Math.acosh()

Повертає гіперболічний арккосинус числа x.

Math.asin()

Повертає арксинус числа x.

Math.asinh()

Повертає гіперболічний арксинус числа.

Math.atan()

Повертає арктангенс числа x.

Math.atan2()

Повертає арктангенс частки переданих аргументів.

Math.atanh()

Повертає гіперболічний арктангенс числа x.

Math.cbrt()

Повертає кубічний корінь числа x.

Math.ceil()

Повертає найменше ціле число, яке дорівнює, або більше за x.

Math.clz32()

Повертає кількість старших нульових бітів 32-бітного цілого числа x.

Math.cos()

Повертає косинус числа x.

Math.cosh()

Повертає гіперболічний косинус числа x.

Math.exp()

Повертає результат обчислення виразу ex, де x — це аргумент, а e — число Ейлера (2.718…, основа натурального логарифма).

Math.expm1()

Повертає результат віднімання 1 від значення exp(x).

Math.floor()

Повертає найбільше ціле число, яке дорівнює, або менше за x.

Math.fround()

Повертає найближчий відповідник x у форматі числа з рухомою комою одинарної точності.

Math.hypot()

Повертає квадратний корінь з суми квадратів переданих аргументів.

Math.imul()

Повертає результат 32-бітного цілочисельного множення аргументів x та y.

Math.log()

Повертає натуральний логарифм (㏒e, або ln) числа x.

Math.log10()

Повертає логарифм аргументу x за основою 10.

Math.log1p()

Повертає натуральний логарифм (㏒e, або ln) значення виразу 1 + x, обчисленого для аргументу x.

Math.log2()

Повертає логарифм аргументу x за основою 2.

Math.max()

Повертає найбільше значення з-поміж нуля чи більше аргументів.

Math.min()

Повертає найменше значення з-поміж нуля чи більше аргументів.

Math.pow()

Повертає результат піднесення x до степеня y (тобто xy).

Math.random()

Повертає псевдовипадкове число від 0 до 1.

Math.round()

Повертає значення аргументу x, округлене до найближчого цілого.

Math.sign()

Повертає знак переданого числа x. Позначає, чи x є додатним, від'ємним, або нулем.

Math.sin()

Повертає синус числа x.

Math.sinh()

Повертає гіперболічний синус числа x.

Math.sqrt()

Повертає додатний квадратний корінь з числа x.

Math.tan()

Повертає тангенс числа x.

Math.tanh()

Повертає гіперболічний тангенс числа x.

Math.trunc()

Повертає цілу частину числа x, відкидаючи всі цифри після коми.

Приклади

Перетворення між градусами і радіанами

Тригонометричні функції: sin(), cos(), tan(), asin(), acos(), atan(), та atan2() — приймають (і повертають) кути в радіанах.

Оскільки люди мають схильність оперувати кутами в градусах, та й деякі функції (наприклад, CSS-трансформації) можуть приймати градуси, корисно мати напохваті функції для переведення значень з градусів у радіани, і навпаки:

function degToRad(degrees) {
  return degrees * (Math.PI / 180);
}

function radToDeg(rad) {
  return rad / (Math.PI / 180);
}

Обчислення висоти рівностороннього трикутника

Якщо потрібно обчислити висоту рівностороннього трикутника, і відомо, що довжина його сторони — 100, — можна застосувати формулу довжина прилеглого катета, помножена на тангенс кута, дорівнює довжині протилежного катета.

Рівносторонній трикутник, у якому перпендикуляр одного ребра проведено з протилежної вершини, утворюючи прямокутний трикутник з трьома сторонами, позначеними як "прилегла", "протилежна" й "гіпотенуза". Кут між "прилеглою" стороною й гіпотенузою – 60 градусів.

В JavaScript можна виконати це обчислення наступним чином:

50 * Math.tan(degToRad(60));

Тут використана функція degToRad() для переведення 60 градусів у радіани, оскільки Math.tan() приймає вхідні значення саме в радіанах.

Повернення випадкового цілого числа між вказаними межами

Це можна зробити, взявши комбінацію з Math.random() і Math.floor():

function random(min, max) {
  const num = Math.floor(Math.random() * (max - min + 1)) + min;
  return num;
}

random(1, 10);

Специфікації

Сумісність із браузерами

desktop mobile server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet Deno Node.js
Math
Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
E Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
LN10 Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
LN2 Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
LOG10E Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
LOG2E Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
PI Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
SQRT1_2 Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
SQRT2 Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
abs Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
acos Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
acosh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
asin Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
asinh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
atan Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
atan2 Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 4
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
atanh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
cbrt Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
ceil Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
clz32 Chrome Full support 38
Edge Full support 12
Firefox Full support 31
Internet Explorer No support No
Opera Full support 25
Safari Full support 7
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 31
Opera Android Full support 25
Safari on iOS Full support 7
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
cos Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
cosh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
exp Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
expm1 Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
floor Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
fround Chrome Full support 38
Edge Full support 12
Firefox Full support 26
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 26
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
hypot Chrome Full support 38
Edge Full support 12
Firefox Full support 27
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 27
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
imul Chrome Full support 28
Edge Full support 12
Firefox Full support 20
Internet Explorer No support No
Opera Full support 16
Safari Full support 7
WebView Android Full support 4.4
Chrome Android Full support 28
Firefox for Android Full support 20
Opera Android Full support 15
Safari on iOS Full support 7
Samsung Internet Full support 1.5
Deno Full support 1.0
Node.js Full support 0.12.0
log Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
log10 Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
log1p Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
log2 Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
max Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
min Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
pow Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
random Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
round Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
sign Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 9
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 9
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
sin Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
sinh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
sqrt Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
tan Chrome Full support 1
Edge Full support 12
Firefox Full support 1
Internet Explorer Full support 3
Opera Full support 3
Safari Full support 1
WebView Android Full support 1
Chrome Android Full support 18
Firefox for Android Full support 4
Opera Android Full support 10.1
Safari on iOS Full support 1
Samsung Internet Full support 1.0
Deno Full support 1.0
Node.js Full support 0.10.0
tanh Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0
trunc Chrome Full support 38
Edge Full support 12
Firefox Full support 25
Internet Explorer No support No
Opera Full support 25
Safari Full support 8
WebView Android Full support 38
Chrome Android Full support 38
Firefox for Android Full support 25
Opera Android Full support 25
Safari on iOS Full support 8
Samsung Internet Full support 3.0
Deno Full support 1.0
Node.js Full support 0.12.0

Дивіться також