# Math functions in JavaScript

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:

## 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

Tags: