Friday, May 26, 2017

JavaScript Arithmetic


 💥The addition operator (+) adds numbers

👊<script>
var x = 10;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>


output 12 

 💥The subtraction operator (-) subtracts numbers

 👊<script>
var x = 5;
var y = 2;
var z = x - y;
document.getElementById("demo").innerHTML = z
</script>


output 3

  💥The multiplication operator (*) multiplies numbers

  👊<script>
var x = 5;
var y = 6;
var z = x * y;
document.getElementById("demo").innerHTML = z; 

</script>
                                  OR
 output 30<script>
var x = 10;
x *= 5;
document.getElementById("demo").innerHTML = x;
</script>

output 50


  💥The modular operator (%) returns the division remainder.

👊 <script>
var x = 5;
var y = 2;
var z = x % y;
document.getElementById("demo").innerHTML = z;
</script>

                                      
output 1

                                    OR
<script>
var x = 10;
x %= 5;
document.getElementById("demo").innerHTML = x;
</script>

output 0


💥The increment operator (++) increments numbers

  👊<script>
var x = 5;
x++;
var z = x;
document.getElementById("demo").innerHTML = z;
</script>


output 6

  💥Operator precedence describes the order in which operations are performed in an arithmetic expression.

  👊<script>
document.getElementById("demo").innerHTML = 100 + 50 * 3;
</script>


output  250


👊 <script>
document.getElementById("demo").innerHTML = (100 + 50) * 3;
</script>


output 450



No comments:

Post a Comment