Javascript Arithmetic Operator Modulus

Introduction

There are three multiplicative operators in Javascript: multiply, divide, and modulus.

If either of the operands for a multiplication operation isn't a number, it is converted to a number using the Number() casting function.

An empty string is treated as 0, and the Boolean value of true is treated as 1.

The modulus (remainder) operator is represented by a percent sign (%):

let result = 26 % 5;  // equal to 1 

The modulus operator behaves differently for special values, as follows:

  • If the operands are numbers, regular arithmetic division is performed.
  • If the dividend is an infinite number and the divisor is a finite number, the result is NaN.
  • If the dividend is a finite number and the divisor is 0, the result is NaN.
  • If Infinity is divided by Infinity, the result is NaN.
  • If the dividend is a finite number and the divisor is an infinite number, then the result is the dividend.
  • If the dividend is zero and the divisor is nonzero, the result is zero.
  • If either operand isn't a number, it is converted to a number behind the scenes using Number() and then the other rules are applied.

for (let i = 1; i < 26; i++) {
  console.log(i + ",");
  if ((i % 5) == 0) {
    console.log("\n");
  }// ww w .ja  va 2 s.  com
}



PreviousNext

Related