Using Math abs() function to coding in Javascript/JS

This JavaScript tutorial explains how to use the math function referred to as abs() with syntax and examples.


In JavaScript, abs() is a function that is used to return the absolute fee of a number. Because the abs() characteristic is a static function of the Math object, it have to be invoked through the placeholder object known as Math.


In JavaScript, the syntax for the abs() feature is:


Parameters or Arguments

number The wide variety to convert to an absolute value.


The abs() function returns the absolute price of a number.

If the abs() characteristic is handed a non-numeric value, an array with one than one element, or an empty object as the range parameter, the abs() function will return NaN (not a number).


The abs() function will try to convert the quantity parameter to a numeric price before calculating the absolute value. Math is a placeholder object that consists of mathematical functions and constants of which abs() is one of these functions.


Let’s take a look at an instance of how to use the abs() function in JavaScript.

For example:

console.log(Math.abs(-30 * 2));

In this example, we have invoked the abs() characteristic the use of the Math class.

We have written the output of the abs() characteristic to the internet browser console log, for demonstration purposes, to exhibit what the abs() function returns.

The following will be output to the web browser console log:


In this example, the first output to the console log lower back 23 which is the absolute cost of -23. The 2d output to the console log also returned 23 after first changing the string fee ‘-23’ to the numeric fee -23 and then calculating its absolute value. The third output to the console log back 60 which is the absolute price of the calculation of -30 x 2