Home >Web Front-end >JS Tutorial >What is the Purpose of the % Operator in JavaScript, and How is it Used?
The % Operator in JavaScript
JavaScript's % operator performs the modulus operation. It calculates the remainder when one number is divided by another and returns the result. This operator is used to check if a number is divisible by another without having to use floating-point division.
Operation:
The modulus operator is used as follows:
<code class="javascript">var1 % var2</code>
Where:
Example:
<code class="javascript">console.log(12 % 5); // 2 console.log(-1 % 2); // -1</code>
Usage Cases:
The % operator has various applications, including:
The above is the detailed content of What is the Purpose of the % Operator in JavaScript, and How is it Used?. For more information, please follow other related articles on the PHP Chinese website!