Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Math Object

JS Math Object

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

Math Object

Unlike other objects, the Math object has no constructor. The Math object is static. All methods and properties can be used without creating a Math object first.

Math Properties (Constants)

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

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

					 
        
          Math.E        // returns Euler's number (2.718281828459045)
          Math.PI       // returns PI (3.141592653589793)
          Math.SQRT2    // returns the square root of 2 (1.4142135623730951)
          Math.SQRT1_2  // returns the square root of 1/2 (0.7071067811865476)
          Math.LN2      // returns the natural logarithm of 2 (0.6931471805599453)
          Math.LN10     // returns the natural logarithm of 10 (2.302585092994046)
          Math.LOG2E    // returns base 2 logarithm of E (1.4426950408889634)
          Math.LOG10E   // returns base 10 logarithm of E (0.4342944819032518)
        
      
Math Methods

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

Math.round()

Math.round(x) returns the nearest integer.

					 
        
          Math.round(4.6);  // 5
          Math.round(4.5);  // 5
          Math.round(4.3);  // 4
          Math.round(-4.6); // -5
          Math.round(-4.5); // -4
          Math.round(-4.3); // -4
        
      
Math.ceil()

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

					 
        
          Math.ceil(4.6);  // 5
          Math.ceil(4.5);  // 5
          Math.ceil(4.3);  // 5
          Math.ceil(-4.6); // -4
          Math.ceil(-4.5); // -4
          Math.ceil(-4.3); // -4
        
      
Math.floor()

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

					 
        
          Math.floor(4.6);  // 4
          Math.floor(4.5);  // 4
          Math.floor(4.3);  // 4
          Math.floor(-4.6); // -5
          Math.floor(-4.5); // -5
          Math.floor(-4.3); // -5
        
      
Math.trunc()

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

					 
        
          Math.trunc(4.6);  // 4
          Math.trunc(4.5);  // 4
          Math.trunc(4.3);  // 4
          Math.trunc(-4.6); // -4
          Math.trunc(-4.5); // -4
          Math.trunc(-4.3); // -4
        
      
Math.sign()

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

					 
        
          Math.sign(4);  // 1 (Positive)
          Math.sign(0);  // 0 (Null, zero)
          Math.sign(-4);  // -1 (Negative)
        
      
Math.pow()

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

					 
        
          Math.pow(4, 2); // 16
          Math.pow(5, 3); // 125
          Math.pow(2, 8); // 256
        
      
Math.sqrt()

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

					 
        
          Math.sqrt(16); // 4
          Math.sqrt(5); // 2.23606797749979
          Math.sqrt(2.3); // 1.51657508881031
        
      
Math.abs()

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

					 
        
          Math.abs(-16); // 16
          Math.abs(5); // 5
          Math.abs(-2.3); // 2.3
        
      
Math.sin()

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

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.

					 
        
          Math.sin(90);  // 0.8939966636005579 (radians)
          Math.sin(90 * Math.PI / 180);  // 1 (degree)
        
      
Math.cos()

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

					 
        
          Math.cos(60);  // -0.9524129804151563 (radians)
          Math.cos(60 * Math.PI / 180);  // 0.5000000000000001 (degree)
        
      
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(0, 150, 30, 20, -8, -200); // -200
          Math.max(0, 150, 30, 20, -8, -200); // 150
        
      
Math.random()

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

					 
        
          Math.random();
        
      
Math.log()

Math.log(x) returns the natural logarithm of x. The natural logarithm returns the time needed to reach a certain level of growth.

					 
        
          Math.log(1); // 0
          Math.log(2); // 0.6931471805599453
          Math.log(3); // 1.0986122886681096
        
      
Math.log2()

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

					 
        
          Math.log2(8); // 3
        
      
Math.log10()

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

					 
        
          Math.log10(100); // 2
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved