Udemy logo

javascript absolute value“Javascript absolute value method, where are you?  I know you’re hiding there somewhere in all of those commands!”

And you’re right!  It really is very easy to return the absolute value of a number in Javascript.

And while we’re on the subject of Javascript, are you looking for a comprehensive online class in Javascript?

(In case you’re a little rusty on your math, the absolute value of a number is just that number’s value without a sign; in practice, it comes down to removing the minus sign from a negative number.)

We know  —  there may be times when when you may not be quite sure what to expect from Javascript, because it was originally designed as a client-side browser scripting language.  But it does do math, and it does it as well as any general-purpose programming language.

Meet the Math Object

The Math object is a native object in Javascript, meaning that it and its methods are always available, regardless of the environment in which Javascript is running. As Javascript objects go, the Math object is pretty simple.  You don’t need to get tangled up with constructors, because (unlike the Date or Array objects) it doesn’t have one. All it has is a bunch of nice, simple properties and methods.  The properties are just constants (things like pi, logarithms, and the square root of 2), and the methods are simply basic mathematical functions  —  including the function for returning an absolute value.

(Does all of this seem a little formidable?  There are plenty of classes in Javascript for absolute beginners to choose from.)

Absolute?  Absolutely!

Here’s how it works.  One of the easiest ways to use a Math object method is to set a variable to equal the method, like this:

var Alice=Math.some_method(some_number);

When you do that, the value of Alice becomes the value that’s returned by some_method operating on some_number.

The absolute value method is just called abs(), so if you want Alice to be the absolute value of -2,743, you’d use this:

var Alice=Math.abs(-2743);

The value of Alice would then be 2,743, without the minus sign.

And of course, since it’s an absolute value, if you use the absolute value function with a positive number, the sign doesn’t change:

var Alice=Math.abs(326);

The value of Alice is now 326.

What Does It Take?

So, what kinds of arguments will abs() take?  Obviously, it will take positive and negative numbers, but it will also take a variety of other types of argument.

For example, it will take an ordinary arithmetic operation as an argument:

var Alice=Math.abs(17.536 - 233);

This sets the value of Alice to 215.464  —  the absolute value of 17.536 – 233 = -215.464.

It will also take other Math objects; the Math object in the argument gets evaluated first, then abs() returns the absolute value:

var Alice=Math.abs(Math.cos(4));

The value of Alice is now set to 0.6536436208636119  —  the absolute value of the cosine of 4 (which is -0.6536436208636119).

And Variables, Too!

And of course, it will take a variable as an argument.  if you first set

var Bob=(-27 * 35);

then take the absolute value of Bob

var Alice=Math.abs(Bob);

The value of Alice is now 945  — the absolute value of -27 * 35 = 945.

It will also take built-in Javascript constants, like null:

var Alice=Math.abs(null);

This returns 0, since null = no value.

Not a Number?!?

But what happens if you give abs() an argument that isn’t a number, or anything that returns a number, or null, such as a string?

var Alice=Math.abs("Don't look at me - I'm just a string!");

When you do this, the Javascript absolute value function will tell you that it’s not a number.  In the above example, the value of Alice will be set to NaN  —  which is Javascript’s way of saying, “Not a Number”.

The absolute value method won’t do everything, of course, because Javascript wasn’t designed to be a programming language for advanced mathematics; you can find out more about its strengths and limits in a definitive tutorial class.  If, for example, you ask it to give you the absolute value of a complex number

var Alice=Math.abs(5 + Math.sqrt(-1));

it will set Alice to NaN, rather than trying to evaluate the complex number.

Putting abs() To Use

What are the practical uses of the Javascript absolute value method?

In practice, of course, you’re probably not going to want to hard-code a number just to get its absolute value, so something like this:

var Alice=Math.abs(-2);

really isn’t very useful.

There are, however, situations where being able to get an absolute value can be useful.  If, for example, you’re designating numbered positions on a line with zero at the midpoint, anything to the left of zero is going to have a negative coordinate.  And if you’re dealing with two-dimensional coordinates (such as positions in a graphic image or a table), you can encounter negative coordinates.

How Far Is Minus Five Miles?

Under those conditions, how do you find the distance between any two points?  You can always check the signs of their coordinates and do the appropriate arithmetic, of course, but a much more general solution is just to subtract one coordinate from another, and take the absolute value of the result.  Consider a simple two-dimensional example:

Jim’s standing at one point on a numbered line with 0 at the midpoint, and Jeff is standing at another point.  How far apart are they?  if Jim is at position 5 and Jeff is at -3, their distance is the absolute value of the difference between the two numbers.

Try it:

var Alice=Math.abs(-3 - 5);


var Alice=Math.abs(5 - -3);

both return a value of 8 for Alice.

And if Jim and Jeff are on the same side of the midpoint, so Jim is at 5 and Jeff is at 3  — or if Jim is at -5 and Jeff is at -3  —  it also works:

var Alice=Math.abs(3 - 5);
var Alice=Math.abs(5 - 3);
var Alice=Math.abs(-3 - -5);
var Alice=Math.abs(-5 - -3);

all set the value of Alice to 2.

It’s a Useful Tool

This is the sort of thing that’s useful whether you’re learning the fundamentals of Javascript or taking an advanced course. You don’t need to know the signs ahead of time, and you don’t need to worry about which number you subtract from which.  You can plug any two coordinates into the formula, take the absolute value, and it will work.

Page Last Updated: April 2014

Top courses in JavaScript

The JavaScript Bible - JavaScript Bootcamp
Bogdan Stashchuk | Software Engineer, MBA, PhD
4.8 (1,517)
The Modern Javascript Bootcamp Course (2022)
Colt Steele, Stephen Grider
4.7 (10,741)
The Creative JavaScript Course
Dev Ed, Maxine Tillman
4.5 (315)
JavaScript: The Advanced Concepts (2023 Update)
Andrei Neagoie, Zero To Mastery
4.7 (11,926)
JavaScript for QA Engineers and SDETs
Glitch IT System
4.6 (854)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
Academind by Maximilian Schwarzmüller, Maximilian Schwarzmüller
4.6 (23,822)
JavaScript Web Projects: 20 Projects to Build Your Portfolio
Andrei Neagoie, Jacinto Wong, Zero To Mastery
4.6 (3,186)
50 Projects In 50 Days - HTML, CSS & JavaScript
Brad Traversy, Florin Pop
4.7 (8,989)

More JavaScript Courses

JavaScript students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo