JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – find numbers difference with math abs

JavaScript Quiz – find numbers difference with math abs

Numbers difference with math abs

In javascript, numbers difference is easy to just subtract the two number but sometime it gives positive difference and sometime negative difference. But Math.abs() is a method which help you in that because it give only positive number.

The Math.abs() function returns the absolute value of a number, that is

Basic Syntax of Math abs method


// Here, x is a number

For more detail about the math abs method click here.

JavaScript Question

In this javascript quiz, find out does give function give numbers difference between two values but math abs also help you to give an unsigned number

First, we declare a javascript function with a name difference and 2 parameters passed to it.

function difference(a, b){
  return Math.abs(a - b);

There is a return statement with Math.abs() and a - b pass to it.

In the next step, we are checking the value return by function difference with different value. And console log will print all the values.

console.log(difference(3, 5));
console.log(difference(5, 3));

Any idea what should be the output above statement.

JavaScript Quiz Test

Here is the full code snippet for you to solve.

function difference(a, b){
  return Math.abs(a - b);

console.log(difference(3, 5)); // output => ?
console.log(difference(5, 3)); // output => ?

Answer – 2, 2

Click To Run Code


Now, you know the answer is “2, 2“. But why, doesn’t the second function console return the 2 also doesn’t it should be -2. Let me explain

So, it’s correct that first function call give 2 and the second function call give -2. But both them gives positive 2.

At last, That’s why we get an answer 2, 2“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – null and undefined act as value in function parameter
  2. JS Quiz – Math random in if condition
  3. JS Quiz – three dots with string variable will expand
  4. JavaScript Quiz – solve 2d array matrix diagonal addition


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
Share via
Copy link