JavaScript Math Object

JavaScript Math object enables mathematical tasks on numbers. 
The properties and methods of Math are static and can be called without creating the Math object.


Math Methods


Method Description
Math.abs(x) Returns the absolute value of a number.
Math.acos(x) Returns the arccosine of a number.
Math.acosh(x) Returns the hyperbolic arccosine of a number.
Math.asin(x) Returns the arcsine of a number.
Math.asinh(x) Returns the hyperbolic arcsine of a number.
Math.atan(x) Returns the arctangent of a number.
Math.atanh(x) Returns the hyperbolic arctangent of a number.
Math.atan2(y, x) Returns the arctangent of the quotient of its arguments.
Math.cbrt(x) Returns the cube root of a number.
Math.ceil(x) Returns the smallest integer greater than or equal to a number.
Math.clz32(x) Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x) Returns the cosine of a number.
Math.cosh(x) Returns the hyperbolic cosine of a number.
Math.exp(x) Returns Ex, where x is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm.
Math.expm1(x) Returns subtracting 1 from exp(x).
Math.floor(x) Returns the largest integer less than or equal to a number.
Math.fround(x) Returns the nearest single precision float representation of a number.
Math.hypot([x[, y[, …]]]) Returns the square root of the sum of squares of its arguments.
Math.imul(x, y) Returns the result of a 32-bit integer multiplication.
Math.log(x) Returns the natural logarithm (loge, also ln) of a number.
Math.log1p(x) Returns the natural logarithm (loge, also ln) of 1 + x for a number x.
Math.log10(x) Returns the base 10 logarithm of a number.
Math.log2(x) Returns the base 2 logarithm of a number.
Math.max([x[, y[, …]]]) Returns the largest of zero or more numbers.
Math.min([x[, y[, …]]]) Returns the smallest of zero or more numbers.
Math.pow(x, y) Returns base to the exponent power, that is, baseexponent.
Math.random() Returns a pseudo-random number between 0 and 1.
Math.round(x) Returns the value of a number rounded to the nearest integer.
Math.sign(x) Returns the sign of the x, indicating whether x is positive, negative or zero.
Math.sin(x) Returns the sine of a number.
Math.sinh(x) Returns the hyperbolic sine of a number.
Math.sqrt(x) Returns the positive square root of a number.
Math.tan(x) Returns the tangent of a number.
Math.tanh(x) Returns the hyperbolic tangent of a number.
Math.toSource() Returns the string "Math".
Math.trunc(x) Returns the integer part of the number x, removing any fractional digits.

Learn more about JavaScript Random

Math Methods Examples


// Math.abs(x) returns the absolute (positive) value of x
Math.abs(-3.14);   // Returns 3.14
 
// Math.ceil(x) returns the value of x rounded up to its nearest integer
Math.ceil(3.14);  // Returns 4
 
// Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).
// Convert degrees to radians if you want to use degrees instead of radians, like this:
// Angle in radians = Angle in degrees x PI / 180.
Math.cos(0 * Math.PI / 180);  // Returns 1 (the cos of 0 degrees)
 
// Math.floor(x) returns the value of x rounded down to its nearest integer
Math.floor(3.14);  // Returns 3
 
// Math.max() is used to find the highest value in a list of arguments
Math.max([x[, y[,]]])
Math.min(920, 70, 30, 0, -15, -5);  // Returns 920
 
// Math.min() is used to find the lowest value in a list of arguments
Math.min(920, 70, 30, 0, -15, -5);  // Returns -15
 
// Math.pow(x, y) returns the value of x to the power of y
Math.pow(5, 2)  // Returns 25
 
// Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):
Math.random();  // Returns a random number
 
// Math.round(x) returns the value of x rounded to its nearest integer
Math.round(3.14);  // Returns 3
Math.round(3.7);   // Returns 4
 
// Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
// Convert degrees to radians if you want to use degrees instead of radians, like this:
// Angle in radians = Angle in degrees x PI / 180.
Math.sin(90 * Math.PI / 180);  // Returns 1 (the sine of 90 degrees)
 
// Math.sqrt(x) returns the square root of x
Math.sqrt(25);  // Returns 5

Note that the trigonometric functions (sin()cos()tan()asin()acos()atan()atan2()) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180), and multiply by this to convert the other way.


Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.


Math Properties


Property Description
Math.E Euler's constant and the base of natural logarithms, approximately 2.718.
Math.LN2 Natural logarithm of 2, approximately 0.693.
Math.LN10 Natural logarithm of 10, approximately 2.303.
Math.LOG2E Base 2 logarithm of E, approximately 1.443.
Math.LOG10E Base 10 logarithm of E, approximately 0.434.
Math.PI Ratio of the circumference of a circle to its diameter, approximately 3.14159.
Math.SQRT1_2 Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
Math.SQRT2 Square root of 2, approximately 1.414.

Math Object Constructor

Unlike other global objects, the Math object has no constructor. Methods and properties are static.

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

 


Learn more about JavaScript Objects Learn more about JavaScript Numbers
BACK TO TOP






Check this out

JS JavaScript: Comparisons

Comparison and Logical operators are used to test for true or false.

JavaScript provides three different value-comparison operations, loose equality, strict equality and Object.is. They have different usage so learn when to use them.

JS JavaScript: Date Formats

There are generally 3 types of JavaScript date input formats:There are generally 3 types of JavaScript date input formats:

  • ISO Date
  • Date Short
  • ....

JS JavaScript: Errors

There will be errors in the code. It is good practice to handle exceptions/errors.

Exceptions are a type of error that occurs during the execution of an application. Errors should be logged. Sometimes it is possible to recover a bug and proceed.
Other cases the user should get inform....

JS JavaScript: Scope

Scope determines if variables are accessible (visible).

A scope in JavaScript defines what variables you have access to. There are two kinds of scope – global scope and local scope.

Function scope and block scope are local scope - variables declared inside a function or a bl....


BACK TO TOP

JavaScript is one of the 3 core language technologies of the World Wide Web that all web developers must learn:

1.      HTML to define the content of web pages.

2.      CSS to specify the layout of web pages.

3.      JavaScript to program the behavior of web pages.

JavaScript is a scripting or programming language that allows you to implement complex things on web pages. Every time a web page does more than just sit there and display static information for you to look at, displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. − you can bet that JavaScript is probably involved.


The benefits of using JavaScript are:

1.      You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.

2.      Immediate feedback to the visitors − They don't have to wait for a page reload to see if they have forgotten to enter something.

3.      Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.

4.      Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.


BACK TO TOP

Need more Honney

NDC Conferences

The NDC Conferences (Norwegian Developers Conference) are one of the world`s largest independent software conferences for .NET & Agile development. The conference covers everything you need to know within software development. The conferences are held each year in many places such as Oslo, Copenhagen, London, Sydney and Minnesota.

Update Conference Prague

Update Conference Prague is the biggest developer conference in the Czech Republic. The conference is offering sessions delivered by the top experts from all around the world. Their goal is to inspire the attendees and enrich their knowledge.

Troy Hunt

Learn about Web Security from Troy Hunt. He is a really engaged speaker that frequently give talks on conferences around the world. Troy is a Microsoft MVP and you can even learn from him on some great courses on Pluralsight.

W3Schools

W3Schools.com is a great education Web Site for learning web technologies. On W3Schools you may find well organized basic to advanced tutorials and references.

Scott Hanselman

Learn about Web Technologies and new trends from Scott Hanselman. Scott Hanselman is an author of developer books and he is an engaged speaker that usually talks about the Microsoft stack. You may meet Scott on conferences around the world.

Developer Week

DeveloperWeek is one of the world’s largest developer conferences. Developer Week focus on new dev technologies, the conference have talks and workshops for newbies and experienced audience. You can attend on a Developer Week conference different places in Nothern America.


BACK TO TOP

Honneybag blog



Learn JavaScript Debugging

Searching for bugs and fixing them is called code debugging. The bugs might be difficult to diagnose and therefore it is important to have a good approach to find and fix the bugs effectively. Bugs in the code might be syntax errors or logical errors. Sometimes nothing happens when the code contains bugs and there is few indications where to search. Modern web browser includes a powerful suite of developer tools. The tools do a range of things...

Explore JavaScript Arrays

Arrays are frequently used by developers. Programming languages can have slightly different implementation of arrays. An array is an object that can store a collection of items. You may say it`s a special variable that can have more than one value. The array holds many values under a single name, and you may access the values by referring to an index number. The easiest way to create....

Ultimate Guide to HTML Colors

It is essential to give the user a good user experience. There are many things you should focus on, one of them is how you use colors on your website. We don't have to struggle with black and white TVs anymore. Users expect more and more and we can apply colors to everything we want. Texts, borders, backgrounds, shadows – in HTML they all can have whatever color you decide....

Wanna be a sheep? Check out the most popular subjects
HONNEYBAG BLOG
HONNEYBAGBLOG INSPIRE-AND-BE-INSPIRED
HTML FORMATTING
JAVASCRIPT ESSENTIALS
HONNEYBAGBLOG EXPLORE-JAVASCRIPT-ARRAYS
Wanna be a little different? Check out the less popular subjects
HONNEYBAG BLOGER
HTML INTRODUCTION
HTML INTRODUCTION
HTML ESSENTIALS
HTML ESSENTIALSWE
BACK TO TOP