Home » Programming » Javascript » Javascript Absolute Value Abs

Calculating the Absolute Value in JavaScript with Math.abs()

This article will explain what absolute values are, how they are used, and how the JavaScript Math.abs() function can be used to calculate the absolute value of a number.

What is the ‘Absolute Value’ of a Number?

The absolute value of a number is that number’s value – without any regard to its sign.

A numbers sign determines whether it is positive or negative – it’s the  symbol before a negative number. So, the absolute value of a number is never negativeConsider the absolute value of a number that numbers distance from 0.

The absolute value of a number is may also be referred to in mathematics and other programming languages as the modulus or magnitude of a number.

Here are some examples to illustrate:

Number Absolute/Modulus Value
4 4
-4 4
-2.5 2.5
x x

When writing mathematical equations, the absolute value is written as |x| (two vertical bars surrounding the number we are taking the absolute value of).

How/Why are Absolute Values Used?

Absolute values are most often used when dealing with distances in mapping/geography, and physics calculations for simulations and games.

When calculating direction or speed, you’re measuring that vector in relation to a fixed point. Movement in one direction may be considered positive, and movement in the opposite direction negative.

In the above scenario, if you’ve moved 30m to the left your position from the fixed point is -30m. However, you can’t be a negative distance from something – there’s no such thing as a negative kilometer – so to find the distance from the fixed point you’d use the absolute value of the new position:

|-30| = 30 

This may seem like a trivial difference – but it is important when calculating an object’s position and movement – if you want to make video games or 3D simulations, you’ll be using it a lot!

JavaScript Math.abs() Function Syntax

The absolute value of a number can be calculated in JavaScript using the Math.abs() function. The syntax is as follows:

Math.abs(NUMBER)

Note that:

  • NUMBER can be any numerical value
  • The function will return the absolute value of the number

Math.abs() Examples

Below is some example usage of Math.abs() – with the expected results:

Math.abs(-3);       // 3
Math.abs(3);        // 3
Math.abs('-3');     // 3 - The string was successfully parsed as a number
Math.abs(null);     // 0 - null has a zero absolute value
Math.abs('');       // 0 - as does an empty string
Math.abs([]);       // 0 - as does an empty array
Math.abs([3]);      // 3 - If an array has a single numeric member, the absolute value of that member will be returned
Math.abs([3, 4]);   // NaN - If an array with more than one member is passed, even if they are all numeric, NaN will be returned
Math.abs({});       // NaN - Objects cannot be parsed as numbers, so NaN will be returned
Math.abs('string'); // NaN - If a string which cannot be parsed as a number is passed, NaN is returned
Math.abs();         // NaN - If no value is passed, NaN is returned

If the value passed to Math.abs() cannot be coerced to a number, the value NaN (Not a Number) will be returned instead.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment