JavaScript Matematik
- Föregående sida JS datuminställningsmetoder
- Nästa sida JS slumpmässig
JavaScript Math-objektet tillåter dig att utföra matematiska uppgifter med numeriska värden.
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
Math.pow()
Math.pow(x, y)
Återvärdet är x till y:e potens:
Instance
Math.pow(8, 2); // Återger 64
Math.sqrt()
Math.sqrt(x)
Återger kvadratroten av x:
Instance
Math.sqrt(64); // Återger 8
Math.abs()
Math.abs(x)
Återger den absoluta (posativa) värdet av x:
Instance
Math.abs(-4.7); // Återger 4.7
Math.ceil()
Math.ceil(x)
Återvärdet är x UpprundningNärmaste heltal:
Instance
Math.ceil(6.4); // Återger 7
Math.floor()
Math.floor(x)
Återvärdet är x NedrundningNärmaste heltal:
Instance
Math.floor(2.7); // Återger 2
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)
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)
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
Instance
Math.max(0, 450, 35, 10, -8, -300, -78); // Returns 450
Math.random()
Math.random()
Return a random number between 0 (inclusive) and 1 (exclusive):
Instance
Math.random(); // Returns a random number
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)
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.
- Föregående sida JS datuminställningsmetoder
- Nästa sida JS slumpmässig