El objeto JavaScript Math le permite realizar tareas matemáticas en números.
Math.PI;
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.PI</h2>
<p>Math.PI returns the ratio of a circle's circumference to its diameter:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.PI;
</script>
</body>
</html>
A diferencia de otros objetos, el objeto Math no tiene constructor.
El objeto Math es estático.
Todos los métodos y propiedades se pueden utilizar sin crear primero un objeto Math.
La sintaxis de cualquier propiedad Math es: Math.property
.
JavaScript proporciona 8 constantes matemáticas a las que se puede acceder como propiedades matemáticas:
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math Constants</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
</body>
</html>
La sintaxis para cualquier método matemático es: Math.método(número)
Hay 4 métodos comunes para redondear un número a un número entero:
Devuelve x redondeado a su entero más cercano
Devuelve x redondeado a su entero más cercano
Devuelve x redondeado hacia abajo a su entero más cercano
Devuelve la parte entera de x (nuevo en ES6)
Math.round()
Math.round(x)
devuelve el número entero más cercano:
Math.round(4.6);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.6);
</script>
</body>
</html>
Math.round(4.5);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.5);
</script>
</body>
</html>
Math.round(4.4);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.4);
</script>
</body>
</html>
Math.ceil()
Math.ceil(x)
devuelve el valor de x redondeado hacia arriba a su entero más cercano:
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.ceil()</h2>
<p>Math.ceil() rounds a number <strong>up</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(4.4);
</script>
</body>
</html>
Math.floor()
Math.floor(x)
devuelve el valor de x redondeado hacia abajo a su entero más cercano:
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.floor()</h2>
<p>Math.floor(x) returns the value of x rounded <strong>down</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
</body>
</html>
Math.trunc()
Math.trunc(x)
devuelve la parte entera de x:
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.trunc()</h2>
<p>Math.trunc(x) returns the integer part of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.trunc(4.7);
</script>
</body>
</html>
Math.sign()
Math.sign(x)
devuelve si x es negativo, nulo o positivo:
Math.sign(-4);
Math.sign(0);
Math.sign(4);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sign()</h2>
<p>Math.sign(x) returns if x is negative, null or positive:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sign(4);
</script>
</body>
</html>
Math.trunc() y Math.sign() se agregaron a JavaScript 2015 - ES6.
Math.pow()
Math.pow(x, y)
devuelve el valor de x elevado a y:
Math.pow(8, 2);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.pow()</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(8,2);
</script>
</body>
</html>
Math.sqrt()
Math.sqrt(x)
devuelve la raíz cuadrada de x:
Math.sqrt(64);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sqrt()</h2>
<p>Math.sqrt(x) returns the square root of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>
</body>
</html>
Math.abs()
Math.abs(x)
devuelve el valor absoluto (positivo) de x:
Math.abs(-4.7);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.abs()</h2>
<p>Math.abs(x) returns the absolute (positive) value of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-4.7);
</script>
</body>
</html>
Math.sin()
Math.sin(x)
devuelve el seno (un valor entre -1 y 1) del ángulo x (dado en radianes).
Si desea utilizar grados en lugar de radianes, debe convertir grados a radianes:
Ángulo en radianes=Ángulo en grados x PI/180.
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sin()</h2>
<p>Math.sin(x) returns the sin of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);
</script>
</body>
</html>
Math.cos()
Math.cos(x)
devuelve el coseno (un valor entre -1 y 1) del ángulo x (dado en radianes).
Si desea utilizar grados en lugar de radianes, debe convertir grados a radianes:
Ángulo en radianes=Ángulo en grados x PI/180.
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.cos()</h2>
<p>Math.cos(x) returns the cosine of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);
</script>
</body>
</html>
Math.min() / Math.max()
Math.min()
y Math.max()
se pueden utilizar para encontrar el valor más bajo o más alto en una lista de argumentos:
Math.min(0, 150, 30, 20, -8, -200);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.min()</h2>
<p>Math.min() returns the lowest value in a list of arguments:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.max(0, 150, 30, 20, -8, -200);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.max()</h2>
<p>Math.max() returns the highest value in a list of arguments.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.random()
Math.random()
devuelve un número aleatorio entre 0 (inclusive) y 1 (exclusivo):
Math.random();
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 and 1:</p>
<p id="demo"></p>
<p>Tip: Click on "Run" several times.</p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>
Aprenderá más sobre Math.random()
en el siguiente capítulo de este tutorial.
Math.log(x)
devuelve el logaritmo natural de x.
El logaritmo natural devuelve el tiempo necesario para alcanzar un determinado nivel de crecimiento:
Math.log(1);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(1);
</script>
</body>
</html>
Math.log(2);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(2);
</script>
</body>
</html>
Math.log(3);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(3);
</script>
</body>
</html>
Math.E y Math.log() son gemelos.
¿Cuántas veces debemos multiplicar Math.E para obtener 10?
Math.log(10);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>How many times must we multiply Math.E to get 10?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(10);
</script>
</body>
</html>
Math.log2(x)
devuelve el logaritmo en base 2 de x.
¿Cuántas veces debemos multiplicar 2 para obtener 8?
Math.log2(8);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log2()</h2>
<p>Math.log2() returns the base 2 logarithm of a number.</p>
<p>How many times must we multiply 2 to get 8?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log2(8);
</script>
</body>
</html>
Math.log10(x)
devuelve el logaritmo en base 10 de x.
¿Cuántas veces debemos multiplicar 10 para obtener 1000?
Math.log10(1000);
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log10()</h2>
<p>Math.log10() returns the base 10 logarithm of a number.</p>
<p>How many times must we multiply 10 to get 1000?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log10(1000);
</script>
</body>
</html>
Devuelve el valor absoluto de x
Devuelve el arcocoseno de x, en radianes
Devuelve el arcocoseno hiperbólico de x
Devuelve el arcoseno de x, en radianes
Devuelve el arcoseno hiperbólico de x
Devuelve el arcotangente de x como un valor numérico entre -PI/2 y PI/2 radianes
Devuelve el arcotangente del cociente de sus argumentos.
Devuelve el arcotangente hiperbólico de x
Devuelve la raíz cúbica de x
Devuelve x, redondeado hacia arriba al entero más cercano
Devuelve el coseno de x (x está en radianes)
Devuelve el coseno hiperbólico de x
Devuelve el valor de Ex
Devuelve x, redondeado hacia abajo al entero más cercano
Devuelve el logaritmo natural (base E) de x
Devuelve el número con el valor más alto
Devuelve el número con el valor más bajo.
Devuelve el valor de x elevado a y
Devuelve un número aleatorio entre 0 y 1
Redondea x al entero más cercano
Devuelve si x es negativo, nulo o positivo (-1, 0, 1)
Devuelve el seno de x (x está en radianes)
Devuelve el seno hiperbólico de x
Devuelve la raíz cuadrada de x
Devuelve la tangente de un ángulo.
Devuelve la tangente hiperbólica de un número.
Devuelve la parte entera de un número (x)
Para obtener una referencia completa, vaya a nuestra Referencia completa de objetos matemáticos.
La referencia contiene descripciones y ejemplos de todas las propiedades y métodos matemáticos.