In JavaScript, a Math object does not need to be created; it exists as a compact mathematical browser coprocessor. Math is always available anywhere in JavaScript code. It includes basic constants in the form of properties and popular mathematical functions in the form of methods.
JavaScript Math is a traditional math, daily essential, accessible and easy to use.
Math Properties
Basic mathematical constants:
- base of the natural logarithm (E);
- natural logarithm (LN2, LN10);
- logarithm of a number (LOG2E, LOG10E);
- Pi number (PI);
- square roots (SQRT1_2, SQRT2).
To access a property of a global JavaScript Math object, specify the name of the object and the name of the required property. For instance,
- var exp = Math.E; // constant e ~ 2.718
- var n314 = Math.PI; // Pi number ~ 3.1415
Usually, variables do not describe in advance, but use the properties of the JavaScript Math object directly in the expression. However, the algorithm may require its own approach. Mathematical constants in the form of properties of the Match object work in any syntax constructs of the JavaScript language.
Positive number and string
If you want to get the absolute value of a number, use 'abs' - a function that makes any number positive.
- Math.abs ('- 65'); // = 65
- Math.abs (19); // = 19
- Math.abs (0); // = 0
To convert a number to a character string for a specific reason, the toString (rdx) function is applied directly to the variable. For instance,- var iPos = (a / b) .toString (10);
- var iPos = (a / b) .toString (2);
- var iPos = (a / b) .toString (8);
- var iPos = (a / b) .toString (16).
'rdx' - , , , , . '(a / b)' .
- . ( ) , .
CSS . JavaScript Math .
. :
- var iPos = Math.abs('-1,3');
.
'NaN'. JavaScript , . :
- var iPos = ('100,33').toString();
: '100,33'. JavaScript Math, .
JavaScript , , .
JavaScript Math . , , .
, CSS, DOM . . () - Math.round, JavaScript : (ceil) (floor).
, . () 10, 100, 1000...
- var x = Math.round(20.5); // = 21
- var x = Math.ceil(1.1); // = 2
- var x = Math.floor(1.1); // = 1
- var x = Math.floor(1.11*10); // =11
math floor javascript . . 10, 100, 1000, ... , .
. , - , , , , , .
: (sin), (cos) (tan), Math (asin), (acos) (atan).
(exp) - "e" , (log), (sqrt) (pow).
round, floor ceil, pow, : - , - , .
, - JavaScript Math, . JavaScript - , , , DOM, .
, , . , , .
,
min max .
: . min , max - .
, 'NaN'.
random 0 1. .
padl(), , . viuq() , , .
, .
, , , random .