JavaScript Numbers and Math Practice Questions with Solutions

Introductions

Numbers are used in JavaScript for calculations such as prices, marks, ages, percentages, measurements, and scores. JavaScript also provides the built-in Math object with useful methods for rounding numbers, finding the largest or smallest value, generating random numbers, and performing mathematical calculations. In this chapter, you will practice common number operations and Math methods through simple, practical examples. JavaScript Numbers and Math Practice questions with solutions help to understand the concepts.

Question 1: Add Two Numbers

Problem

Create two variables containing 25 and 35, then calculate and display their sum.

Solution

letnumber1=25;letnumber2=35;letsum=number1+number2;console.log(sum);

Output

60

Step-by-step Explanation

  1. number1 stores 25.
  2. number2 stores 35.
  3. The + operator adds both numbers.
  4. 25 + 35 gives 60.
  5. The result is stored in sum.
  6. console.log() displays the result.

Question 2: Calculate the Remainder

Problem

Create two numbers, 25 and 4, and find the remainder after dividing the first number by the second number.

Solution

letnumber1=25;letnumber2=4;letremainder=number1%number2;console.log(remainder);

Output

1

Step-by-step Explanation

  1. number1 contains 25.
  2. number2 contains 4.
  3. The % operator returns the remainder.
  4. 25 ÷ 4 gives a remainder of 1.
  5. Therefore, the output is 1.

Question 3: Round a Decimal Number

Problem

A calculation produces the value 7.6. Use Math.round() to round it to the nearest whole number.

Solution

letnumber=7.6;letresult=Math.round(number);console.log(result);

Output

8

Step-by-step Explanation

  1. The variable number contains 7.6.
  2. Math.round() rounds a number to the nearest integer.
  3. Since 7.6 is closer to 8 than 7, the result is 8.

Question 4: Round a Number Down

Problem

A number is 8.9. Use Math.floor() to round it down to the nearest whole number.

Solution

letnumber=8.9;letresult=Math.floor(number);console.log(result);

Output

8

Step-by-step Explanation

  1. number contains 8.9.
  2. Math.floor() rounds a number down to the nearest integer.
  3. The result is 8.

For positive numbers:

8.9 → 8

Question 5: Round a Number Up

Problem

A number is 8.1. Use Math.ceil() to round it up to the nearest whole number.

Solution

letnumber=8.1;letresult=Math.ceil(number);console.log(result);

Output

9

Step-by-step Explanation

  1. number contains 8.1.
  2. Math.ceil() rounds a number up to the next integer.
  3. Therefore, 8.1 becomes 9.

Question 6: Find the Largest Number

Problem

Three students score 78, 92, and 85 marks. Use Math.max() to find the highest score.

Solution

letmarks1=78;letmarks2=92;letmarks3=85;lethighest=Math.max(marks1, marks2, marks3);console.log(highest);

Output

92

Step-by-step Explanation

  1. Three variables store the three scores.
  2. Math.max() compares the values provided to it.
  3. The largest value is 92.
  4. The result is stored in highest.
  5. The program displays 92.

Question 7: Find the Smallest Number

Problem

Three temperatures are 28, 22, and 31 degrees. Use Math.min() to find the lowest temperature.

Solution

lettemperature1=28;lettemperature2=22;lettemperature3=31;letlowest=Math.min(temperature1, temperature2, temperature3);console.log(lowest);

Output

22

Step-by-step Explanation

  1. The three temperature values are stored in variables.
  2. Math.min() finds the smallest value.
  3. The smallest temperature is 22.
  4. The result is stored in lowest.
  5. The program displays 22.

Question 8: Calculate the Square of a Number

Problem

Create a variable containing 6 and calculate its square using Math.pow().

Solution

letnumber=6;letsquare=Math.pow(number, 2);console.log(square);

Output

36

Step-by-step Explanation

  1. number stores 6.
  2. Math.pow(number, 2) means 6 raised to the power of 2.
  3. The calculation is:
6 × 6 = 36
  1. The result is stored in square.
  2. The program displays 36.

You can also use the exponentiation operator:

letsquare=number**2;

Question 9: Generate a Random Number

Problem

Generate a random decimal number between 0 and 1 using JavaScript.

Solution

letrandomNumber=Math.random();console.log(randomNumber);

Output

The output will be different each time, for example:

0.724583921

Step-by-step Explanation

  1. Math.random() generates a random number.
  2. The generated number is greater than or equal to 0.
  3. It is less than 1.
  4. Because it is random, the exact output changes each time you run the program.

For example:

0.210.650.930.47

are all possible results.


Question 10: Generate a Random Number from 1 to 10

Problem

Write a JavaScript program that generates a random whole number between 1 and 10.

Solution

letrandomNumber=Math.floor(Math.random() *10) +1;console.log(randomNumber);

Output

The output can be any whole number from:

12345678910

Step-by-step Explanation

  1. Math.random() generates a number from 0 up to, but not including, 1.
  2. Multiplying it by 10 gives a value from 0 up to, but not including, 10.
  3. Math.floor() removes the decimal part.
  4. The result becomes a whole number from 0 to 9.
  5. Adding 1 changes the range to 1 through 10.

The basic formula is:

Math.floor(Math.random() *10) +1

This technique is commonly used in games, quizzes, random selections, and other JavaScript programs.

Key Takeaways

  • JavaScript uses the number data type for numeric values.
  • The + operator performs addition.
  • The - operator performs subtraction.
  • The * operator performs multiplication.
  • The / operator performs division.
  • The % operator returns the remainder.
  • Math.round() rounds a number to the nearest integer.
  • Math.floor() rounds down.
  • Math.ceil() rounds up.
  • Math.max() finds the largest value.
  • Math.min() finds the smallest value.
  • Math.pow() calculates a number raised to a power.
  • Math.random() generates a random decimal number between 0 and 1.
  • Math.floor() and Math.random() can be combined to generate random whole numbers.

FAQs

1. What is the Math object in JavaScript?

Math is a built-in JavaScript object that provides properties and methods for performing mathematical operations.

2. What does Math.round() do?

Math.round() rounds a number to the nearest whole number.

console.log(Math.round(5.6));

Output:

6

3. What is the difference between Math.floor() and Math.ceil()?

Math.floor() rounds down, while Math.ceil() rounds up.

console.log(Math.floor(5.8));console.log(Math.ceil(5.2));

Output:

56

4. How can I find the largest number in JavaScript?

Use Math.max().

console.log(Math.max(10, 25, 15));

Output:

25

5. How can I find the smallest number in JavaScript?

Use Math.min().

console.log(Math.min(10, 25, 15));

Output:

10

6. What does Math.random() return?

Math.random() returns a pseudo-random decimal number greater than or equal to 0 and less than 1.

7. How can I generate a random number between 1 and 10?

You can use:

letnumber=Math.floor(Math.random() *10) +1;console.log(number);

The result will be a whole number from 1 to 10.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top