• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Math abs JavaScript – How to get the absolute value of a number using JavaScript

Math abs JavaScript – How to get the absolute value of a number using JavaScript

January 6, 2022 Leave a Comment

To get the absolute value of a number we can use the Math abs JavaScript method, Math.abs().

var num = Math.abs(-7);

The above code would return the number 7.

Some other examples of Math.abs() are below:

var num = Math.abs(7);
var num1 = Math.abs(.6789);
var num2 = Math.abs(0);
var num3 = Math.abs(-50);
var num4 = Math.abs(-50.98283);

Which would result in the following:

7
.6789
0
50
50.98283

Math.abs() in action using jQuery

Below we will provide code to let the user input a number, and then use the Math.abs() method on that number. Here is our simple HTML setup:

<p>Type a number you want to use the Math.abs() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

Below is the JavaScript and jQuery code which take the user input using the jQuery click() or on() keypress methods, and use the Math.abs() method on that user input and update the results below using the jQuery text() method.

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Math.abs($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.abs($("#userVal").val()));
});

The final code and output for this example is below:

Code Output:

Type a number you want to use the Math.abs() method on below:


Full Code:

<p>Type a number you want to use the Math.abs() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

<script>

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Math.abs($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.abs($("#userVal").val()));
});

</script>

Hopefully this article has been useful in helping you understand how the Math.abs() method works using JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Change the Font Size of a Paragraph
  • 2.  Using JavaScript to Convert Float to Integer
  • 3.  JavaScript join – Create String from Elements in an Array
  • 4.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 5.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 6.  Using JavaScript to Convert Double to Integer
  • 7.  How to Count Vowels in a String Using JavaScript
  • 8.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 9.  charAt() JavaScript – Getting the Index Position of a Character
  • 10.  How to Remove All Numbers From String in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy