-3

I am trying to decipher a piece of JS code (while (obviously) my JS knowledge is null).
What is the % operator in the code below?

   m[p1%2][q1] = 0.0;  
   for(j=q1+1; j <= q2; j++)
      m[p1%2][j] = m[p1%2][j-1] + 1;
Enamul Hassan
  • 4,744
  • 22
  • 35
  • 52
Z80
  • 19,117
  • 19
  • 138
  • 260
  • 5
    [__Remainder__](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()) – Rayon May 03 '16 at 13:20
  • 3
    It seems the reason for a downvote was the lack of research effort, which is one of the main reasons for downvotes. And it definitely looks like a duplicate, 4 other people appear to agree. – Jerry Dodge May 03 '16 at 13:29
  • FYI, the `%` operator is the same (or at least very similar) in Javascript as it is in Java, C, C++, C#, Python, Perl, and doubtless other languages as well. I think C was the first to use it as the modulo/remainder operator. – Mike Harris May 03 '16 at 13:43
  • asking 'What is the % operator in JavaScript?' on StackOverflow does not show the other question. StackOverflow search engine sucks. – Z80 May 03 '16 at 13:51
  • @Tushar: Both questions talk about the same thing, `%`. If you think it's not a duplicate because the other question uses the wrong terminology we can simply update the other question ;) – Felix Kling May 03 '16 at 13:53
  • @FelixKling Right, the terminology is not used in questions so, it is dupe. – Tushar May 03 '16 at 13:55
  • Thanks to all (Tushar, Feathercrown, Avan) that posted a meaningful answer instead a random/useless comment about this question. All receive an upvote from me. – Z80 May 10 '16 at 13:58

3 Answers3

6

% is remainder operator. It gives the remainder after division. Quoting from MDN

The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2.

There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.

Tushar
  • 78,625
  • 15
  • 134
  • 154
2

% is modulo (or modulus?). It divides the first term by the second and returns the remainder. This can be useful for looping through a set of numbers and many other things.

Usage: x % y

Examples:

5 % 10; //5
10 % 5; //0
16 % 5; //1
Feathercrown
  • 1,886
  • 1
  • 13
  • 23
1

% is a Mathematical operator, it is used for Modulus. in simple 3 % 2=1 Modulus operator returns reminder.