Javascript Math

7 min read

The JavaScript Math object allows you to perform mathematical tasks on numbers.

JavaScript Math объект нь тоон дээр математикийн даалгавар гүйцэтгэх боломжийг олгодог.

Example Жишээ #

Math.PI; // returns 3.141592653589793

The Math Object #

Unlike other objects, the Math object has no constructor.

Бусад объектуудаас ялгаатай нь Math объект нь ямар ч байгуулагчгүй байдаг.

The Math object is static.

Математикийн объект нь хөдөлгөөнгүй байна.

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

Бүх арга, шинж чанарыг эхлээд Math объект үүсгэхгүйгээр ашиглаж болно.


Math Properties (Constants) #

The syntax for any Math property is : Math.property.

Математикийн аливаа өмчийн синтакс нь: Math.property.

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

JavaScript нь Математикийн шинж чанараар нэвтрэх боломжтой 8 математик тогтмолыг өгдөг.

Example Жишээ #

Math.E // returns Euler's number Math.PI // returns 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 base 2 logarithm of E Math.LOG10E // returns base 10 logarithm of E

Math Methods #

The syntax for Math any methods is : Math.method.(number)

Математикийн аливаа аргын синтакс нь: Math.method.(number)


Number to Integer #

There are 4 common methods to round a number to an integer:

Тоог бүхэл тоогоор бөөрөнхий болгох нийтлэг 4 арга байдаг:

Math.round(x) Returns x rounded to its nearest integer

X-ийг хамгийн ойрын бүхэл тоогоор буцаана

Math.ceil(x) Returns x rounded up to its nearest integer

X-ийг хамгийн ойрын бүхэл тоогоор буцаана

Math.floor(x) Returns x rounded down to its nearest integer

X-ийг хамгийн ойрын бүхэл тоогоор буцаана

Math.trunc(x) Returns the integer part of x (new in ES6)

X-ийн бүхэл хэсгийг буцаана (ES6-д шинэ)


Math.round() #

Math.round(x) returns the nearest integer:

Math.round(x) нь хамгийн ойрын бүхэл тоог буцаана:

Example Жишээ #

Math.round(4.9); // returns 5 Math.round(4.7); // returns 5 Math.round(4.4); // returns 4 Math.round(4.2); // returns 4 Math.round(-4.2); // returns -4

Math.ceil() #

Math.ceil(x) returns the value of x rounded up to its nearest integer:

Math.ceil(x) нь x-ийн утгыг хамгийн ойрын бүхэл тоогоор буцаана:

Example Жишээ #

Math.ceil(4.9); // returns 5 Math.ceil(4.7); // returns 5 Math.ceil(4.4); // returns 5 Math.ceil(4.2); // returns 5 Math.ceil(-4.2); // returns -4

Math.floor() #

Math.floor(x) returns the value of x rounded down to its nearest integer:

Math.floor(x) нь x-ийн утгыг хамгийн ойр бүхэл тоогоор буцаана:

Example Жишээ #

Math.floor(4.9); // returns 4 Math.floor(4.7); // returns 4 Math.floor(4.4); // returns 4 Math.floor(4.2); // returns 4 Math.floor(-4.2); // returns -5

Math.trunc() #

Math.trunc(x) returns the integer part of x:

Math.trunc(x) нь x-ийн бүхэл хэсгийг буцаана:

Example Жишээ #

Math.trunc(4.9); // returns 4 Math.trunc(4.7); // returns 4 Math.trunc(4.4); // returns 4 Math.trunc(4.2); // returns 4 Math.trunc(-4.2); // returns -4

Math.sign() #

Math.sign(x) returns if x is negative, null or positive:

Math.sign(x) нь x сөрөг, тэг эсвэл эерэг байвал буцаана:

Example Жишээ #

Math.sign(-4); // returns -1 Math.sign(0); // returns 0 Math.sign(4); // returns 1

Math.trunc() and Math.sign() were added to JavaScript 2015 – ES6.

Math.trunc () ба Math.sign () нь JavaScript 2015 – ES6 дээр нэмэгдсэн.


 

Math.pow() #

Math.pow(x, y) returns the value of x to the power of y:

Math.pow (x, y) нь x-ийн утгыг y-ийн хүчинд буцаана:

Example Жишээ #

Math.pow(8, 2); // returns 64

Math.sqrt() #

Math.sqrt(x) returns the square root of x:

Math.sqrt(x) нь x-ийн квадрат язгуурыг буцаана.

Example Жишээ #

Math.sqrt(64); // returns 8

Math.abs() #

Math.abs(x) returns the absolute (positive) value of x:

Math.abs(x)нь x-ийн үнэмлэхүй (эерэг) утгыг буцаана:

Example Жишээ #

Math.abs(-4.7); // returns 4.7

Math.sin() #

Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).

Math.sin(x) нь x өнцгийн синусыг (-1 ба 1-ийн хоорондох утга) буцааж өгдөг (радиан дээр өгөгдсөн).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Хэрэв та радианы оронд градус ашиглахыг хүсвэл градусыг радиан болгон хөрвүүлэх хэрэгтэй.

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

Радиан дахь өнцөг = x PI / 180 градусын өнцөг.

Example Жишээ #

Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)

Math.cos() #

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

Math.cos (x) нь x өнцгийн косинусыг (-1 ба 1-ийн хоорондох утга) буцааж өгдөг (радиан дээр өгөгдсөн).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Хэрэв та радианы оронд градус ашиглахыг хүсвэл градусыг радиан болгон хөрвүүлэх хэрэгтэй.

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

Радиан дахь өнцөг = x PI / 180 градусын өнцөг.

Example Жишээ #

Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)

Math.min() and Math.max() #

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:

Math.min()ба Math.max() -г аргументуудын жагсаалтаас хамгийн бага эсвэл хамгийн их утгыг олоход ашиглаж болно.

Example Жишээ #

Math.min(0, 150, 30, 20, -8, -200); // returns -200

Example Жишээ #

Math.max(0, 150, 30, 20, -8, -200); // returns 150

Math.random() #

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

Math.random() нь 0 (хамааруулсан) ба 1 (онцгой) хоорондох санамсаргүй тоог буцаана:

Example Жишээ #

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

You will learn more about Math.random() in the next chapter of this tutorial.

Та Math.random() талаар илүү ихийг энэ гарын авлагын дараагийн бүлгээс авах болно.


The Math.log() Method #

Math.log(x) returns the natural logarithm of x:

Math.log(x) нь x-ийн натурал логарифмыг буцаана.

Example Жишээ #

Math.log(1); // returns 0

The natural logarithm returns the time needed to reach a certain level of growth.

Байгалийн логарифм нь тодорхой өсөлтийн түвшинд хүрэхэд шаардагдах хугацааг буцаадаг.

Math.E and Math.log() are twins.

Math.E ба Math.log () бол ихрүүд юм.

How many times must we multiply Math.E to get 10?

Бид Math.E-ийг хэдэн удаа үржүүлж 10-ыг авах ёстой вэ?

Example Жишээ #

Math.log(10); // returns 2.302585092994046

The Math.log2() Method #

Math.log2(x) returns the base 2 logarithm of x.

Math.log2(x) нь x-ийн үндсэн 2 логарифмыг буцаана.

How many times must we multiply 2 to get 8?

8-ыг авахын тулд бид 2-ыг хэдэн удаа үржүүлэх ёстой вэ?

Example Жишээ #

Math.log2(8); // returns 3

The Math.log10() Method #

Math.log10(x) returns the base 10 logarithm of x.

Math.log10(x) нь x-ийн 10 үндсэн логарифмыг буцаана.

How many times must we multiply 10 to get 1000?

1000 гарахын тулд бид 10-ыг хэдэн удаа үржүүлэх ёстой вэ?

Example Жишээ #

Math.log10(1000); // returns 3

Math Object Methods #

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
acosh(x) Returns the hyperbolic arccosine of x
asin(x) Returns the arcsine of x, in radians
asinh(x) Returns the hyperbolic arcsine of x
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
atanh(x) Returns the hyperbolic arctangent of x
cbrt(x) Returns the cubic root of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, …, n) Returns the number with the highest value
min(x, y, z, …, n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sign(x) Returns if x is negative, null or positive (-1, 0, 1)
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of x
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a number
trunc(x) Returns the integer part of a number (x)

Complete Math Reference #

For a complete reference, go to our complete Math object reference.

Бүрэн лавлагаа авахын тулд манай Математикийн объектын бүрэн лавлагаа руу орно уу.

The reference contains descriptions and examples of all Math properties and methods.

Математикийн бүх шинж чанар, аргуудын тодорхойлолт, жишээг агуулсан болно.

Powered by BetterDocs

Leave a Reply