JavaScript Reference: Mathematics and Numbers


Math Properties & Predefined Constants

PropertyDescriptionApproximate Value
Math.EEuler's constant, \(e\)2.718
Math.LN2Natural (base \(e\)) logarithm of 22.303
Math.LN10Natural (base \(e\)) logarithm of 101.443
Math.LOG10EBase 10 logarithm of \(e\)0.434
Math.PIRatio of circle's circumference to its diameter3.14159
Math.SQRT1_2Square root of 1/20.707
Math.SQRT2Square root of 21.414

Math Methods

MethodReturnsExampleExample ReturnsNotes
Math.abs(x)Absolute value (distance from 0) of \(x\)Math.abs(-6.5)6.5
Math.acos(x)Angle \(\theta\) (in radians) such that \(\cos(\theta)=x\)Math.acos(0.6)0.9272952180016123\(x\) must be between -1 and 1
Math.asin(x)Angle \(\theta\) (in radians) such that \(\sin(\theta)=x\)Math.asin(0.6)0.6435011087932844\(x\) must be between -1 and 1
Math.atan(x)Angle \(\theta\) (in radians) such that \(\tan(\theta)=x\)Math.atan(10)1.4711276743037345
Math.atan2(x,y)Angle \(\theta\) (in radians) such that \(\tan(\theta)=x/y\)Math.atan2(20,2)1.4711276743037345\(y\) cannot be 0
Math.ceil(x)Smallest integer greater than \(x\)Math.ceil(4.23)5
Math.cos(x)\(\cos(x)\)Math.cos(Math.PI)-1\(x\) is an angle in radians
Math.exp(x)\(e^x\)Math.ceil(4.23)4
Math.floor(x)Greatest integer less than \(x\)Math.ceil(4.23)4
Math.log(x)Natural logarithm of \(x\)Math.log(1)0\(x\) must be a positive number
Math.max(list)Largest value in listMath.max(4,6,1,9,2,-5,2)9
Math.min(list)Smallest value in listMath.min(4,6,1,9,2,-5,2)-5
Math.pow(x,y)\(x^y\)Math.pow(4,2)16Math.pow(x,-y) = \(\frac{1}{x^y}\)
Math.pow(x,a/b) = \(\sqrt[b]{x^a}\)
Math.random()Pseudo-random value between 0 and 1Math.random()0.37454011430963874
Math.round(x)Closest integer to \(x\)Math.round(3.14159)3
Math.sin(x)\(\sin(x)\)Math.sin(Math.PI/2)1\(x\) is an angle in radians
Math.sqrt(x)Square root of \(x\)Math.sqrt(25)5\(x\) is nonnegative number
Math.tan(x)\(\tan(x)\)Math.tan(6)-0.29100619138474915\(x\) is an angle in radians

Number Properties

PropertyDescriptionApproximate Value
Number.EPSILONSmallest interval between two numbers (used to test 'close enough' equality:
x-y < Number.EPSILON returns true if x and y are 'within epsilon' of each other)
\(2^{-52}\)
Number.MAX_VALUELargest possible representable number1.7976931348623157e+308
Number.MIN_VALUESmallest possible representable number5e-324
Number.NaN"Not a number"; equivalent to NaN
Number.NEGATIVE_INFINITYNegative infinity, returned on overflow
Number.POSITIVE_INFINITYPositive infinity, returned on overflow

Number Instances


InstanceReturnsExampleExample Returns
object.toExponential()String representing number object in exponential notationvar x=523423.3452;
x.toExponential()
'5.234233452e+5'
object.toFixed()String representing number object in fixed point notationvar x=523423.3452;
x.toFixed()
'523423'
object.toPrecision(x)String representing number object with precision \(x\)var x=523423.3452;
x.toPrecision(3)
'5.23e+5'
object.toString()String representing number object in base 10var x=523423.3452;
x.toString()
'523423.3452'
object.toString(b)String representing number object in base \(b\)var x=523423;
x.toString(2)
'1111111110010011111'

Random Integers Between Two Values

The helpful function below takes in two parameters, a and b. It returns a random integer between a and b.
function randomBetween( a,b ) {
	return Math.floor(Math.random()*(b-a+1)+a);
}

Operators

Arithmetic Operators

OperatorDescriptionExampleExample Returns
+Addition, String concatenation3+6
"hello "+"world"
9
"hello world"
-Subtraction12-84
/Division12/34
*Multiplication2*510
%Modulus (remainder)17%52
++Increase by onevar x = 5; x++x is now 6
+xUnary plus, evaluates operand and converts to number, if possible+"45"
+true
45
1

Comparison Operators

OperatorDescriptionExampleExample Returns
x == yReturns true if x and y can be converted to the same type and value5 == 5
5 == '5'
1 == true
true
false
true
x != yReturns true if x and y are of different types or values5 != 5
5 != '5'
1 != true
false
true
false
x === yReturns true if x and y are the same type and value5 === 5
5 === '5'
1 === true
true
false
false
x !== yReturns true if x and y are not the same type and value5 !== 5
5 !== '5'
1 !== true
false
true
true
x > yReturns true if x is strictly greater than y after type conversion5>2
6>'3'
2>5
true
true
false
x >= yReturns true if x is greater than or equal to y after type conversion5>=7
6>='3'
5>=5
false
true
true
x < yReturns true if x is strictly less than y after type conversion5<2
6<'10'
2<5
false
true
true
x <= yReturns true if x is less than or equal to y after type conversion5<=7
6<='3'
5<=5
true
false
true

Assignment Operators

OperatorDescriptionExampleExample Returns
x = valueAssign a value to a variable xvar x = 5, y = "hello"
console.log(x,y)
5 'hello'
x += yAddition assignment, shorthand for x = x+yx = 7; x += 5 x is now 12
x -= ySubtraction assignment, shorthand for x = x-yx = 7; x -= 5 x is now 2
x *= yMultiplication assignment, shorthand for x = x*yx = 7; x *= 5 x is now 35
x /= yDivision assignment, shorthand for x = x/yx = 10; x /= 5 x is now 2
x %= yRemainder assignment, shorthand for x = x%yx = 10; x %= 3 x is now 1

Logical Operators

OperatorDescriptionExampleExample Returns
x && yReturns true if both x AND y are true(3 < 4) && (4 == 4)
(3 < 4) && (4 > 4)
true
false
x || yReturns true if either x OR y are true(3 < 4) || (4 == 4)
(3 < 4) || (4 > 4)
(2 > 6) || (2 > 4)
true
true
false
!xReturns true if x is false!(3 < 4)
!(3 > 4)
false
true

Conditional Operator

The conditional operator takes three operands: a condition that can be evaluated to true or false, an expression to return if the condition is true, and a condition to return if the condition is false. The syntax of the conditional operator is
(condition to test) ? expression_if_true : expression_if_false

var x = prompt( "Pick a number" )

// Decide if provided value is larger than 10, and choose a message accordingly:
var returnMessage = ( x > 10 ) ? " larger than 10" : " less than or equal to 10"
alert( "The number " + x + " is " + returnMessage )
Try it
var x = prompt( "Find the absolute value of any number: " )
var absX = ( x >= 0 ) ? x : -x
alert( "The absolute value of " + x + " is " + absX )
Try it