In this article on How to Use Math Functions in JavaScript, I will explain the functions that the Math object offers.
In fact, Math object offers several functions and properties that help us in performing various Math related tasks. The following list provides the properties that the Math object provides. Later an example of using these properties is also provided.
- PI
- E
- SQRT2
- SQRT1_2
- LN2
- LN10
- LOG2E
- LOG10E
Example of Using Math Functions in JavaScript
<script>
document.write("Pi: "+Math.PI+"<br>");
document.write("e: "+Math.E+"<br>");
document.write("Sqrt(2): "+Math.SQRT2+"<br>");
document.write("Sqrt(1/2): "+Math.SQRT1_2+"<br>");
document.write("ln2: "+Math.LN2+"<br>");
document.write("ln10: "+Math.LN10+"<br>");
document.write("Log2e: "+Math.LOG2E+"<br>");
document.write("Log10e: "+Math.LOG10E+"<br>");
</script>
Output
Apart from properties, the Math object also offers several functions. The following list specifies the functions offered by the Math object.
- round(). Basically, the round() function takes a value as parameter and rounds of it to the nearest integer.
- ceil(). Like, the round() function, ceil() function also returns an integer for a fractional value. However, it returns the smallest integer greater than the given number.
- floor(). Likewise, the floor() function returns the largest integer smaller than the given value.
- trunc(). Unlike, ceil() and floor(), the trunc() function just truncates the fractional part of the value.
- sign(). The sign() function return 1 for a positive value, and -1 for a negative value. For 0 value, it returns 0.
- pow(). The pow() function computes and returns power of a number.
- sqrt(). Similarly, the sqrt() function returns the square root of its parameter.
- abs(). In order to find the absolute value of a number, we use this function. For example, Math.abs(-199) returns 199.
- min(). This function returns minimum from a list of values.
- max(). Likewise, this function returns maximum from a list of values.
- random(). For the purpose of finding a random number, we use this function.
- sign(). In order to find the sign of a number, we use this function. When number is positive, it returns 1, if it is negative it returns 0. For 0, it returns 0.
- sin(). This function in JavaScript returns the sine of
x
, wherex
is an angle in radians. - cos(). Similarly, this function in JavaScript returns the cosine of
x
, wherex
is an angle in radians. - log(). This function returns the natural logarithm of the given number.
- log10(). Similarly this function returns the base-10 logarithm of the given number.
- tan(). Likewise, this function in JavaScript returns the tangent of
x
, wherex
is an angle in radians. - tanh(). Whereas this function in JavaScript returns the hyperbolic tangent of
x
.
In order to find examples of Math Functions in JavaScript, click here.
Further Reading
Evolution of JavaScript from ES1 to ES2020
Introduction to HTML DOM Methods in JavaScript
Understanding Document Object Model (DOM) in JavaScript
Understanding HTTP Requests and Responses
What is Asynchronous JavaScript?
JavaScript Code for Event Handling
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML