MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Arithmetic


Constants


ConstantsDescriptionApproximate
Math.E Base of natural logarithm e 2.718
Math.LN10Natural logarithm of 10 2.302
Math.LN2 Natural logarithm of 2 0.693
Math.PI Pi: the ratio of circle circumference to diameter (π) 3.14
Math.SQRT1_2 Square root of 1/2 0.707
Math.SQRT2 Square root of 2 1.414
Number.MAX_VALUE Largest positive finite value of Number 1.79E+308
Number.MIN_VALUE Smallest positive value for Number 5E-324
Infinity Value of positive infinity (∞) 0---

Remainder / Modulus (%)

The remainder / modulus operator (%) returns the remainder after (integer) division.


console.log( 42 % 10);
console.log( 42 % -10);
console.log(-42 % 10); 
console.log(-42 % -10);
console.log(-40 % 10); 
console.log( 40 % 10); 

Output

2
2
-2
-2
-0
0

This operator returns the remainder left over when one operand is divided by a second operand. When the first operand is a negative value, the return value will always be negative, and vice versa for positive values.
In the example above, 10 can be subtracted four times from 42 before there is not enough left to subtract again without it changing sign. The remainder is thus: 42 - 4 * 10 = 2.

The remainder operator may be useful for the following problems:


Rounding

Math.round() will round the value to the closest integer using half round up to break ties.


var a = Math.round(2.3); 
var b = Math.round(2.7); 
var c = Math.round(2.5); 

Output

a is now 2
b is now 3
c is now 3

But


var c = Math.round(-2.7);
var c = Math.round(-2.5);

Output

c is now -3
c is now -2

Rounding up

Math.ceil() will round the value up.


var a = Math.ceil(2.3); 
var b = Math.ceil(2.7); 

Output

a is now 3
b is now 3

ceiling a negative number will round towards zero


var c = Math.ceil(-1.1); 

Output

c is now 1

Rounding down

Math.floor() will round the value down.


var a = Math.ceil(2.3);
var b = Math.ceil(2.7);

Output

a is now 3
b is now 3

ceiling a negative number will round towards zero


var c = Math.ceil(-1.1);

Output

c is now 1


Conclusion

In this page (written and validated by ) you learned about Javascript Arithmetic . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Trigonometry.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.