JavaScript provides a wide range of built-in math functions that allow you to perform various mathematical operations. Here are some commonly used math functions in JavaScript:
Function | Description |
---|---|
Math.abs(x) | Returns the absolute value of x |
Math.ceil(x) | Rounds x up to the nearest integer |
Math.floor(x) | Rounds x down to the nearest integer |
Math.round(x) | Rounds x to the nearest integer |
Math.max(x1, x2, ..., xn) | Returns the largest of the given numbers |
Math.min(x1, x2, ..., xn) | Returns the smallest of the given numbers |
Math.pow(x, y) | Returns x raised to the power of y |
Math.sqrt(x) | Returns the square root of x |
Math.random() | Generates a random number between 0 and 1 (exclusive) |
Math.sin(x) | Returns the sine of an angle x (in radians) |
Math.cos(x) | Returns the cosine of an angle x (in radians) |
Math.tan(x) | Returns the tangent of an angle x (in radians) |
Math.log(x) | Returns the natural logarithm (base e) of x |
Math.exp(x) | Returns e raised to the power of x |
Math.floor(Math.random() * (max - min + 1)) + min | Generates a random integer between min and max (inclusive) |
Math functions in JavaScript example
Simple example code showcasing the usage of math functions in JavaScript:
1. Finding the absolute value of a number:
let x = -5;
let absX = Math.abs(x);
console.log(absX);
2. Rounding a number up:
let x = 3.7;
let roundedUp = Math.ceil(x);
console.log(roundedUp);
3. Generating a random number between 0 and 1:
let randomNum = Math.random();
console.log(randomNum);
4. Calculating the square root of a number:
let x = 16;
let sqrtX = Math.sqrt(x);
console.log(sqrtX);
5. Generating a random integer between a given range:
let min = 1;
let max = 10;
let randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomInt);
6. Calculating the sine of an angle:
let angle = Math.PI / 6; // 30 degrees in radians
let sineValue = Math.sin(angle);
console.log(sineValue);
Here’s an example that includes multiple use cases of math functions in JavaScript:
// Finding the absolute value of a number
let x = -5;
let absX = Math.abs(x);
console.log(absX); // Output: 5
// Rounding a number up
let y = 3.7;
let roundedUp = Math.ceil(y);
console.log(roundedUp); // Output: 4
// Generating a random number between 0 and 1
let randomNum = Math.random();
console.log(randomNum); // Output: a random decimal between 0 and 1
// Calculating the square root of a number
let z = 16;
let sqrtZ = Math.sqrt(z);
console.log(sqrtZ); // Output: 4
// Generating a random integer between a given range
let min = 1;
let max = 10;
let randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomInt); // Output: a random integer between 1 and 10 (inclusive)
// Calculating the sine of an angle
let angle = Math.PI / 6; // 30 degrees in radians
let sineValue = Math.sin(angle);
console.log(sineValue); // Output: 0.5 (approximately)
Output:
You can run this code in a JavaScript environment (such as a browser console or a Node.js environment) to see the corresponding outputs.
Comment if you have any doubts or suggestions on this JS Math function list tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version