We can use JavaScript to check if a number is divisible by 3 by using the JavaScript built-in remainder operator %. We divide the number by 3; if the remainder after division is 0, then the number is divisible by 3. If it is not 0, then the number is not divisible by 3.

Here is the JavaScript code to check this:


if ((num % 3) == 0){
  return true; //number is divisible 3
} else { 
  return false; //number is NOT divisible by 3
}

We can put this code in a function that will check if a number is divisible by 3 in JavaScript.

function isDivisibleBy3(num){
  if ((num % 3) == 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 another number.

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

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

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

console.log(isDivisibleBy3(10));
console.log(isDivisibleBy3(15));
console.log(isDivisibleBy3(167225));
console.log(isDivisibleBy3(-6));
console.log(isDivisibleBy3(0));

#Output:
false
true
false
true
true

Checking if a Number is Divisible by Another Number in JavaScript

We can use JavaScript to check if a number is divisible by another number by also 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 with some examples:

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

Check if a Number is Divisible by 2

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

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

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

#Output:
true
false

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 3.

Categorized in:

JavaScript,

Last Update: March 12, 2024