Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Remainder assignment(%=) Operator
  • Difficulty Level : Hard
  • Last Updated : 02 Mar, 2021

The remainder assignment operator (%=) assigns the remainder to the variable after dividing a variable by the value of the right operand.

Syntax:

Operator: x %= y
Meaning:  x  = x % y

Below example illustrate the Remainder assignment(%=) Operator in JavaScript:

Example 1: The following example demonstrates if the given number is divisible by 4 or if it’s an even or odd number.

Javascript




<script>
  let num = 16;
  
  //test if its divisible by 4
  if (num % 4 == 0) {
    console.log(true);
  }
  //test for even number
  if (num % 2 == 0) {
    console.log(true);
  } else {
    console.log(false);
  }
  
  //test for odd number
  if (!(num % 2 == 0)) {
    console.log(true);
  } else {
    console.log(false);
  }
</script>

Output:

true
true
false

Example 2:

Javascript




<script>
  let gfg = 3;
  
  console.log((gfg %= 2));
  
  console.log((gfg %= 0));
  
  console.log((gfg %= "world"));
</script>

Output:

1
Nan
Nan



My Personal Notes arrow_drop_up
Recommended Articles
Page :