How to Calculate Absolute Value in JavaScript

Instructor: Lonny Meinecke

Lonny teaches psychology classes at King University, and has a bachelor's degree in IT and a doctorate in psychology.

In this lesson, we will calculate the absolute value of a number using JavaScript. We will define what absolute value means, show how to calculate it, and share how to use the built-in JavaScript Math object, which makes it easier.

Definition of JavaScript

JavaScript is a programming language that is often used in creating web pages. JavaScript allows the web page designer to do really dynamic things, rather than simply format and display text and images. For example, with JavaScript, the web page can respond to user input and interact with the web page visitor. Another of those dynamic things is that JavaScript can do some math for you.

Definition of Absolute Value

In case you are not acquainted with it, the absolute value of a number is the distance that the number is from zero on a number line, regardless of whether it is above or below the number zero. That means the sign of the number (+ or -) does not matter, all we care about is the value itself, not its sign. For example, the absolute value of +5 or -5 is just 5.

Need a metaphor? Think of your absolute value as how far away you are, no matter which direction you go (east or west). If you are 3 klicks away from your home base (a klick is 1 kilometer), does it matter if you are -3 clicks to the left (the red dot going west in the image) or +3 clicks to the right (the green dot going east in the image)? You are still 3 klicks in any direction. That's kind of like your absolute value.

A compass heading metaphor for absolute value
compass example

Examples That Calculate Absolute Value

As with many programming languages, there is more than one way to calculate absolute value in JavaScript. We will show you a fast, old way first without using any JavaScript object-oriented programming (OOP):

var a = -5;
var b;
b = a < 0 ? a * -1 : a;

The code works like this. The first line creates the variable a and initializes it to -5. The second line creates the variable b, but we don't initialize it. The third line does the work. This is called a ternary operation. If the variable a is less than 0, we multiply it by -1 (because a negative times a negative is always positive) and store that in b. If the number isn't less than 0, we can just leave it alone because it must be either 0 or positive. This example results in the answer 5 for b. Easy enough?

There was a time when this might have been a faster way to calculate absolute value, but experts today share that there is very little difference in speed, and using OOP is easier. So, we will show you the newer, easier way.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support