Math.abs()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La función Math.abs() retorna el valor absoluto de un número, que es

Math.abs(x)=|x|={xifx>00ifx=0-xifx<0{\mathtt{\operatorname{Math.abs}(x)}} = {|x|} = \begin{cases} x & \text{if} \quad x \geq 0 \ -x & \text{if} \quad x < 0 \end{cases}

Sintaxis

Math.abs(x)

Parámetros

x

Un número.

Valor de retorno

El valor absoluto del número dado.

Descripción

Como abs() es un método estático de Math, deberías siempre usar Math.abs(), en lugar de un método de un objeto Math que crees (Math no es un constructor).

Ejemplos

Comportamiento de Math.abs()

Pasando un string no-numérico o una variable undefined/empty retorna NaN. Pasando null retorna 0.

js
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN

Especificaciones

Specification
ECMAScript® 2025 Language Specification
# sec-math.abs

Compatibilidad con navegadores

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
abs

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Ver también