JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Math


📘 JavaScript Math – Performing Mathematical Operations

JavaScript has a built-in Math object that allows you to perform mathematical calculations, including rounding, finding min/max, generating random numbers, and more.

📌 The Math object is not a constructor, so you don’t use new Math().


🔹 Common Math Methods

Method Description Example
Math.round(x) Rounds x to the nearest integer Math.round(4.6)5
Math.floor(x) Rounds x down Math.floor(4.9)4
Math.ceil(x) Rounds x up Math.ceil(4.1)5
Math.trunc(x) Removes the decimal (keeps integer part) Math.trunc(4.9)4
Math.pow(x, y) x to the power of y Math.pow(2, 3)8
Math.sqrt(x) Square root of x Math.sqrt(25)5
Math.abs(x) Absolute value (no sign) Math.abs(-5)5
Math.min(x, y, ...) Minimum value among arguments Math.min(1, 5, 0)0
Math.max(x, y, ...) Maximum value among arguments Math.max(1, 5, 0)5
Math.random() Random number between 0 and 1 Math.random()0.34

🔹 Rounding Summary

Function Description Math.round(4.5) Math.ceil(4.5) Math.floor(4.5) Math.trunc(4.5)
Round to nearest 5 5 5 4 4

🔹 Generating Random Integers

// Random number between 0 and 9
Math.floor(Math.random() * 10);

// Random number between 1 and 100
Math.floor(Math.random() * 100) + 1;

Practice Questions

Q1. How do you round the number 4.6 to the nearest integer using Math.round()?

Q2. How do you get the square root of 49 using a Math method?

Q3. What is the output of Math.floor(5.9) and what does it represent?

Q4. How do you round up the number 4.1 using Math.ceil()?

Q5. How do you raise 2 to the power 5 using Math.pow()?

Q6. How do you generate a random number between 0 and 1 using Math?

Q7. How do you get the maximum number from a list like (4, 9, 1) using Math?

Q8.How do you convert -10 into a positive number using Math.abs()?

Q9. What does Math.trunc(4.7) return and why?

Q10. How do you generate a random integer between 1 and 50 in JavaScript?


JS Math Quiz

Q1: What does Math.round(4.5) return?

A. 4
B. 5
C. 4.5
D. NaN

Q2: What is the output of Math.floor(4.9)?

A. 5
B. 4
C. 4.9
D. NaN

Q3: What is returned by Math.pow(3, 2)?

A. 5
B. 9
C. 6
D. 8

Q4: Which method gives the square root of a number?

A. Math.pow()
B. Math.sqrt()
C. Math.abs()
D. Math.random()

Q5: What does Math.abs(-100) return?

A. -100
B. 0
C. 100
D. undefined

Q6: What is the result of Math.ceil(5.2)?

A. 5
B. 6
C. 5.2
D. NaN

Q7: What does Math.random() return?

A. A random number between 0 and 1
B. An integer
C. Always 0.5
D. A negative number

Q8: What is the purpose of Math.trunc()?

A. Round to nearest integer
B. Remove decimal part
C. Return absolute value
D. Return smallest integer

Q9: Which method gives the largest number from a group?

A. Math.max()
B. Math.high()
C. Math.ceil()
D. Math.up()

Q10: Which of the following will return a random integer between 1 and 10?

A. Math.random() * 10
B. Math.floor(Math.random()) + 10
C. Math.floor(Math.random() * 10) + 1
D. Math.random(1, 10)

Go Back Top