• 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 / Using JavaScript to Check if Number is Divisible by Another Number

Using JavaScript to Check if Number is Divisible by Another Number

July 8, 2022 Leave a Comment

We can use JavaScript to check if a number is divisible by another number by using the JavaScript built-in remainder operator %. If the remainder after division is 0, then the number is divisible by the number you divided by.


if ((x % y) == 0){
  return true; //number is divisible by other number
} else { 
  return false; //number is NOT divisible by other number
}

We can wrap this code and create our own simple function that takes two numbers. The first number will be the one we want to divide by the second one. Here is the our function:

function isDivisible(num1, num2){
  if ((num1 % num2) == 0){
    return true;
  } else { 
    return false;
  }
};

When working with numbers in JavaScript, it can be useful to know if the numbers you are working with are divisible by certain numbers.

We can use the JavaScript built-in remainder operator % to get the remainder of a number after division.

If the remainder after division is 0, then the number is divisible by the number you divided by.

Below is our function again. We will add some test examples to see what the function will return.

function isDivisible(num1, num2){
  if ((num1 % num2) == 0){
    return true;
  } else { 
    return false;
  }
};

console.log(isDivisible(10,2));
console.log(isDivisible(15,6));
console.log(isDivisible(167225,25));
console.log(isDivisible(-4,-2));

#Output:
true
false
true
true

How to Check if a Number is Divisible by Another Number

Using the JavaScript remainder operator %, we can determine if a number is divisible by any other number.

For example, to check if a number is divisible by 2 using JavaScript, we divide by 2. If the remainder after division is 0, then the number is divisible by 2. If it is not 0, then the number is not divisible by 2.

Below is a function which will check if a number is divisible by 2 in JavaScript.

function isDivisibleBy2(num){
  if ((num % 2) == 0){
    return true;
  } else { 
    return false;
  }
};

console.log(isDivisibleBy2(10));
console.log(isDivisibleBy2(15));

#Output:
true
false

If we want to check if a number is divisible by 3, just put 3 after the % operator in our function above.

function isDivisibleBy3(num){
  if ((num % 3) == 0){
    return true;
  } else { 
    return false;
  }
};

console.log(isDivisibleBy3(10));
console.log(isDivisibleBy3(15));

#Output:
false
true

How to Check if a Number is Even or Odd Using JavaScript

We can also use % to check if a number is even or odd very easily with the JavaScript built in remainder operator %. If the remainder of a number after dividing by 2 is 0, then the number is even. If not, the number is odd.

function isEven(num){
    if ((num % 2) == 0){
    return true;
  } else { 
    return false;
  }
};

console.log(isEven(10));
console.log(isEven(15));

#Output:
true
false

Hopefully this article has been useful for you to learn how to use JavaScript to check if a number is divisible by another.

Other Articles You'll Also Like:

  • 1.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 2.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 3.  Using JavaScript to Get New Date Format in dd mm yy
  • 4.  Add Hours to a Date Using JavaScript
  • 5.  Using the getElementsByClassName Method in JavaScript
  • 6.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 7.  setTimeout javascript – How the setTimeout() Method in JavaScript Works
  • 8.  Check if Character is Uppercase in JavaScript
  • 9.  Changing the Background Image of a div in JavaScript
  • 10.  JavaScript Random Number – How to Generate a Random Number 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