The JavaScript Maths.abs() method provides a utility to attain the absolute value of a number. The abs() method is an absolute function of maths and therefore, it is used as the Math.abs() method. Absolute value is a positive value or a value without negation.

Math.abs() method belongs to ECMAScript1 and is supported by all the browsers. This guide will describe the detailed knowledge of the JavaScript Math.abs() method with the following outcomes.

– How does JavaScript Math.abs() method works

– How to use the JavaScript Math.abs() method

How does JavaScript Math.abs() method works

Math.abs() is a static function of JavaScript and returns the absolute value. Let’s refer to the following syntax of the Math.abs() method.

Here, ‘n’ is a parameter and Math.abs() retrieves the absolute value of ‘n’.

Math.abs() method returns the same output if the ‘n’ is positive or zero. However, if the given number is negative then it removes the negation and returns a positive number.

How to use the JavaScript Math.abs() method

Javascript Math.abs() function is commonly used to acquire the absolute value of a given number. It refers to its distance from the minimum value on the number line without any direction.

How to use the Math.abs() method with the positive number

Here, we’ll show you how Math.abs() method behaves when a positive number is passed to it.

value = Math.abs(37);

console.log(value);

In the above code, a positive value ‘37’ is passed to the Math.abs() method and the result of the Math.abs() method is stored in a variable.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-65.png" data-lazy- height="114" src="data:image/svg xml,” width=”554″>

The output showed that the function returned the same value as an output because the Math.abs() function returns the absolute values only. Therefore, the returned output number is ‘37’.

How to use the Math.abs() method with the negative number

This example demonstrates the working of the Math.abs() method if a negative number is passed to it.

value = Math.abs(18);

console.log(value);

In this code, the negative value ‘-18’ is passed as a parameter to the Math.abs() function.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-67.png" data-lazy- height="113" src="data:image/svg xml,” width=”554″>

The output showed that the function returned the positive value of the ‘18’. We know that the Math.abs() function only returns the absolute values.With the negative values, this function removes the negation sign and converts them into positive values.

How to use the Math.abs() method with the null value

The following code is practised to understand the working of Math.abs() in presence of a null value.

value = Math.abs(null);

console.log(value)

In the above code, a null value is passed to the Math.abs() function.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-69.png" data-lazy- height="129" src="data:image/svg xml,” width=”554″>

The output showed that the Math.abs() function returned the ‘0’ when a null value is passed to a parameter. It is the property of Math.abs() method that the function returns the ‘0’ for an empty string.

Conclusion

In JavaScript, the Math.abs() function is used to obtain the absolute value of a number. Math.abs() changes the sign of negative number. However, it returns the same number in case of a positive number or zero value. This post demonstrates the working and usage of the Math.abs() method in JavaScript.