Dùng Math.abs(x) trả về giá trị tuyệt đối (luôn ≥ 0).
Ví dụ
<html>
<head>
<title>Phương thức Math.abs trong JavaScript</title>
</head>
<body>
<script>
document.write("abs(-1) = " + Math.abs(-1) + "<br>"); // 1
document.write("abs(null) = " + Math.abs(null) + "<br>"); // 0
document.write("abs(20) = " + Math.abs(20) + "<br>"); // 20
document.write("abs('string') = " + Math.abs("string")); // NaN
</script>
</body>
</html>