• 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
  • VBA
  • 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.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 2.  Adding an Underline with JavaScript
  • 3.  Using JavaScript to Redirect After 5 Seconds
  • 4.  Remove Undefined Values From an Array in JavaScript
  • 5.  Using JavaScript to Get the Height of an Element
  • 6.  JavaScript value – Get the Value from an Input Field
  • 7.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 8.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 9.  Using JavaScript to Get the Current URL
  • 10.  Using JavaScript to Compare Dates

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy