Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Math

The JavaScript Math object sports a set of built-in methods and constants for performing mathematical calculations in scripts.

Constants(Properties)

E - Euler's constant. The base of Natural Logarithms. LN2 - Natural logarithm of 2. LN10 - Natural logarithm of 10. LOG2E - Base 2 logarithm of E. LOG10E - Base 10 logarithm of E. PI - Ratio of a circle's circumference to its diameter. SQRT1_2 - Square root of 1/2. SQRT2 - Square root of 2. var eul = Math.E; // 2.718281828459045 var ln2 = Math.LN2; // 0.6931471805599453 var ln10 = Math.LN10; // 2.302585092994046 var log2e = Math.LOG2E; // 1.4426950408889634 var log10e = Math.LOG10E; // 0.4342944819032518 var pi = Math.PI; // 3.141592653589793 var sq1_2 = Math.SQRT1_2; // 0.7071067811865476 var sq2 = Math.SQRT2; // 1.4142135623730951

Trigonometric Formulas

Formula for calculating radians: radians = degrees * Math.PI/180 Formula for calculating degrees from radians: degrees = radians * 180/Math.PI

Methods

ceil - round a decimal number up to a whole number regardless of decimal value. var num = 5.3; var result = Math.ceil(num); document.write(result); // 6 floor - round a decimal number down to a whole number regardless of decimal value. var num = 2.8; var result = Math.floor(num); document.write(result); // 2 max - returns the largest number out of a set of numbers. var result = Math.max(3,8,5,2); document.write(result); // 8 min - returns the smallest number out of a set of numbers. var result = Math.min(600,800,500,900); document.write(result); // 500 random - returns a random decimal number between "0" and "1". var result = Math.random(); document.write(result); // 1 - 10 random number (multiply random() by 10, round down and add 1) var rand_1_thru_10 = Math.floor(Math.random() * 10) + 1; document.write(rand_1_thru_10); round - round a decimal number naturally. var num = 7.3; var result = Math.round(num); document.write(result); // 7 sqrt - returns the square root of a number. var num = 144; var result = Math.sqrt(num); document.write(result); // 12

Trigonometric Methods of the Math Object

abs - returns the distance a number must go to reach 0. acos - returns the arc cosine of a number(number range: -1.0 to 1.0). asin - returns the arc sine of a number (number range: -1.0 to 1.0). atan - returns the arctangent of a number. atan2 - returns the arctangent of the quotient of its arguments(Ypoint,Xpoint). cos - returns the cosine of the number (returns a value between -1 and 1). log - returns the natural logarithm of a number. pow - returns base number to the exponent power. sin - returns the sine of a number. tan - returns the tangent of a number. var num_abs = Math.abs(aNumber); var num_acos = Math.acos(aNumber); var num_asin = Math.asin(aNumber); var num_atan = Math.atan(aNumber); var num_atan2 = Math.atan2(Ypoint,Xpoint); var num_cos = Math.cos(aNumber); var num_log = Math.log(aNumber); var num_pow = Math.pow(base,exponent); var num_sin = Math.sin(aNumber); var num_tan = Math.tan(aNumber);

JavaScript

Objects

ArgumentsArrayAudioBooleanDataTransferDateDocumentElementFunctionGlobalHistoryHTMLElementMathNavigatorNumberObjectRegExpScreenStorageStringWindowXMLHttpRequest