How to find if the variable is divisible by 2 with JavaScript?

Estimated read time 1 min read

You can use the modulo operator % in JavaScript to check if a number is divisible by 2. The modulo operator returns the remainder of dividing one number by another, so if the result is 0, the first number is divisible by the second number.

Here’s an example:

let num = 4;

if (num % 2 === 0) {
  console.log(`${num} is divisible by 2`);
} else {
  console.log(`${num} is not divisible by 2`);

This will output 4 is divisible by 2.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply