JavaScript Matematik

JavaScript Math-objektet tillåter dig att utföra matematiska uppgifter med numeriska värden.

Instance

Math.PI;           // Återger 3.141592653589793

Try it yourself

Math.round()

Math.round(x) Återvärdet är x avrundat till närmaste heltal:

Instance

Math.round(6.8);    // Återger 7
Math.round(2.3);    // Återger 2

Try it yourself

Math.pow()

Math.pow(x, y) Återvärdet är x till y:e potens:

Instance

Math.pow(8, 2);      // Återger 64

Try it yourself

Math.sqrt()

Math.sqrt(x) Återger kvadratroten av x:

Instance

Math.sqrt(64);      // Återger 8

Try it yourself

Math.abs()

Math.abs(x) Återger den absoluta (posativa) värdet av x:

Instance

Math.abs(-4.7);     // Återger 4.7

Try it yourself

Math.ceil()

Math.ceil(x) Återvärdet är x UpprundningNärmaste heltal:

Instance

Math.ceil(6.4);     // Återger 7

Try it yourself

Math.floor()

Math.floor(x) Återvärdet är x NedrundningNärmaste heltal:

Instance

Math.floor(2.7);    // Återger 2

Try it yourself

Math.sin()

Math.sin(x) Återger sinus av vinkeln x (mätt i radianer) (ett värde mellan -1 och 1).

If you want to use degrees instead of radians, you need to convert the angle to radians:

Angle in radians = Angle in degrees x PI / 180.

Instance

Math.sin(90 * Math.PI / 180);     // Återger 1 (sinus av 90 grader)

Try it yourself

Math.cos()

Math.cos(x) Return the cosine of angle x (in radians) (a value between -1 and 1).

If you want to use degrees instead of radians, you need to convert the angle to radians:

Angle in radians = Angle in degrees x PI / 180.

Instance

Math.cos(0 * Math.PI / 180);     // Returns 1 (cosine of 0 degrees)

Try it yourself

Math.min() and Math.max()

Math.min() and Math.max() Can be used to find the lowest or highest value in the parameter list:

Instance

Math.min(0, 450, 35, 10, -8, -300, -78);  // Returns -300

Try it yourself

Instance

Math.max(0, 450, 35, 10, -8, -300, -78);  // Returns 450

Try it yourself

Math.random()

Math.random() Return a random number between 0 (inclusive) and 1 (exclusive):

Instance

Math.random();     // Returns a random number

Try it yourself

You will learn more about Math.random() knowledge.

Math properties (constants)

JavaScript provides 8 mathematical constants accessible by the Math object:

Instance

Math.E          // Returns Euler's number (Euler's number)
Math.PI         // Returns the value of pi (PI)
Math.SQRT2      // Returns the square root of 2
Math.SQRT1_2    // Returns the square root of 1/2
Math.LN2        // Returns the natural logarithm of 2
Math.LN10       // Returns the natural logarithm of 10
Math.LOG2E      // Returns the logarithm of e to the base 2 (approximately 1.414)
Math.LOG10E     // Returns the logarithm of e to the base 10 (approximately 0.434)

Try it yourself

Math constructor

Unlike other global objects, the Math object does not have a constructor. Methods and properties are static.

All methods and properties (constants) can be used without first creating a Math object.

Math object method

Method Description
abs(x) Return the absolute value of x
acos(x) Return the inverse cosine of x, in radians
asin(x) Returnerar arcsin av x, mätt i radianer
atan(x) Returnerar arctangent av x med ett värde mellan -PI/2 och PI/2 radianer
atan2(y,x) Returnerar vinkeln från x-axeln till punkten (x,y)
ceil(x) Överskatta x
cos(x) Returnerar cosinus av x
exp(x) Returnerar värdet av Ex
floor(x) Underskatta x
log(x) Returnerar den naturliga logaritmen av x (bas e)
max(x,y,z,...,n) Returnerar högsta värdet
min(x,y,z,...,n) Returnerar lägsta värdet
pow(x,y) Returnerar x till y-pow
random() Returnerar ett slumpmässigt tal mellan 0 och 1
round(x) Rundar x till närmaste heltal
sin(x) Returnerar sinus av x (x mätt i grader)
sqrt(x) Returnerar kvadratroten av x
tan(x) Returnerar vinkeln tanget

Fullständig Math-referenshandbok

För en fullständig referenshandbok, besök vår fullständiga Math-objektets referenshandbok.

Denna referenshandbok innehåller beskrivningar och exempel på alla Math-attribut och metoder.