javascript interactive

JavaScript Interactive — a console crash course


1.7 Mathematical Calculations

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
1 + 2 * 3
7
Arithmetic operators +, -, * and / all work as expected. Normal mathematical order of operations is used.
>>>
1.0E308 * 10
Infinity
Sometimes the numerical representation is just not enough.
>>>
1E309 * 1E-308 * -1
-Infinity
>>>
Number.MAX_VALUE
1.7976931348623157e+308
The Number.MAX_VALUE & Number.MIN_VALUE constants provide access to the floating-point extremes.
>>>
5 / 3
1.6666666666666667
>>>
5 % 3
2
Integer math is possible by using the % (modulo) operator.
>>>
- (1 + 1)
-2
The + and - operators are also unary, meaning that they can be applied to a single value.
>>>
Math.round(1.5)
2
A bunch of common mathematical functions are available in the built-in Math object.
>>>
isFinite(NaN) || isFinite(Infinity)
false
The global isFinite, isNaN, parseInt & parseFloat built-in functions are also useful. See the Mozilla JavaScript Reference for all details.
>>>

Index 1 2 3 4 5 6 7