Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to find if variable is divisible by 2 with JavaScript.

In this article, we’ll look at how to find if variable is divisible by 2 with JavaScript.

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

To find if variable is divisible by 2 with JavaScript, we can use the modulo operator.

For instance, we write

const divisibleBy2 = variable % 2 === 0;

to check if variable is divisible by 2 with

variable % 2 === 0

If variable is divisible by 2, then we get 0 as the remainder if we divide variable by 2.

% returns the remainder when we divide the left operand by the right one.

Conclusion

To find if variable is divisible by 2 with JavaScript, we can use the modulo operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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