JavaScript Absolute Value

JavaScript Absolute Value | The technique of the Math object in JavaScript that enables us to get the absolute value of a number is called JavaScript absolute.

The Math.abs() method consistently gives the same numeric values without any negative sign, regardless of whether the provided number is positive or negative. That is, regardless of whether we give a positive or negative number, the result is always a positive number.

When a 0 (zero) number is supplied to the Math.abs() function, the zero value is returned. The Math.abs() method was first created and used in the ECMAScript 1 version of JavaScript. In this article, we’ll look at the Math.absolute method, its syntax, usage, and operation, as well as some examples.

JavaScript Absolute Value Syntax

The abs() function in JavaScript has the following syntax:- Math.abs(numericalValue);

Math is a JavaScript entity that has many methods and attributes that aid in the execution of various mathematical calculations. The static method abs() is one of the functions available in the Math object. As a result, whenever you want to utilize the abs() function, you must first mention the Math term, followed by the abs() method. For example, Math.abs();

numericalValue: An integer for whom the absolute value needs to be found.

The absolute value of any numericalValue supplied as an argument to the abs() method is returned as ReturnValue.

JavaScript Absolute Value Example

In JavaScript, math is an object, not a function Object(), and abs() is indeed one of the static methods. As a result, we always employ the absolute technique with Math.abs(). When a two-element array or a blank object is used, or a non-numeric string is provided as a parameter to the Math.abs() approach, or even an empty variable or unspecified variable is provided as a parameter to the abs() method, the absolute function’s return type is NaN, meaning Not a Number.

Example:

console.log(Math.abs(60));
console.log(Math.abs('-85'));
console.log(Math.abs(-13 * 3));
console.log(Math.abs())
console.log(Math.abs(0))
console.log(Math.abs(''))
console.log(Math.abs([]))
console.log(Math.abs([74]))
console.log(Math.abs([90, 80, 70]))
console.log(Math.abs(undefined))

We used the Math class to call the abs() function in this example. To demonstrate what the abs() method returns, we have printed the result of the abs() method on the web terminal log.

Output:-

60
85
39
NaN
0
0
0
74
NaN
NaN

Explanation:

The very first output to the console log, in this case, was 60, which would be the absolute value of -60. After translating the string value ‘-85’ to the integer value -85 and then computing its absolute value, the second output to the console log also showed 85. The third console log output produced 39, which is the absolute value of the -13 x 3 computation.

All positive/negative integer numbers return their equal positive value as that of the absolute value, as can be seen in the above result. The Math.abs() procedure will provide a zero (0) as the output if the input is zero, or a blank string, or a blank array. If the given parameter is an array with several numbers as elements, a blank object, or a string value that cannot be converted to a numerical value, or if the parameter is undefined or left blank, it will return NaN.

In JavaScript, the math.abs() method is used to get the absolute value of an integer. We must be cautious when implementing it because using improper values including arrays, strings, objects, and so on can result in undesirable outputs. As with the Math.abs() method, we must always call the abs() function with the Math keyword prefixed to it.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *