Modulus Operator (%)

Summary

Divides two numbers and returns the remainder.

Syntax

expression1 % expression2

Parameters

expression1
An expression that evaluates to a number.
expression2
An expression that evaluates to a number.

Description

For numeric data, the percent % symbol is the modulus operator. The modulus operator divides two numbers and returns the remainder. For example, 5 % 2 will evaluate to the result 1. If the division has no remainder, the modulus operation will return zero (0); for example, 4 % 2 will return 0.

The modulus operator takes the sign of the first expression. Thus, -11 % 3 yields -2.

Differences from JavaScript

  • In JS++, arithmetic cannot be performed on Booleans. In JavaScript, for these operations, false is treated as 0 and true is treated as 1.

  • In JS++, arithmetic operations cannot be performed on null. In JavaScript, null is treated as 0.

  • In JS++, undefined cannot be used in arithmetic operations. In addition, NaN can never occur from integer arithmetic. In JavaScript, an arithmetic expression that evaluates to undefined or NaN results in NaN for the result.

Examples

Basic Usage
1
2
3
4
import System;
 
Console.log(4 / 2); // 2
Console.log(8 / 2); // 4

See Also

Share

HTML | BBCode | Direct Link