abs math JavaScript JS

The JavaScript math abs() method is used to get the absolute value of a number.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Math.abs(n)
Math.abs(n)
Math.abs(n)

Parameters
n: It represents the number whose absolute value has to be obtained.

Returns
The absolute value of a number

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
document.writeln(Math.abs(-345));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> document.writeln(Math.abs(-345)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
document.writeln(Math.abs(-345));
</script>
</body>
</html>