Chapter 10

JavaScript Numbers

Numbers are used for calculations and numeric data such as age, marks, prices, quantities, and percentages.

What is JavaScript Numbers?

  • JavaScript numbers can represent integer and decimal values.
  • Arithmetic operators can be used to perform calculations with numbers.
  • Common numeric values use the JavaScript Number type.
  • NaN means Not-a-Number, and Infinity is also a possible numeric result.

Syntax

let age = 20;
let price = 499.99;
let total = 100 + 50;

Example

Try understanding this code before running it.
let price = 500;
let quantity = 3;
let total = price * quantity;

console.log(total);

Important Points

  • Decimal values are also part of the Number type.
  • Arithmetic operators are used to perform calculations.
  • NaN can appear when a numeric operation produces an invalid numeric result.
  • Number.isNaN() can be used to check whether a value is NaN.

Common Mistakes

  • Treating a numeric string as the same as a number.
  • Performing calculations on user input without converting it when necessary.
  • Ignoring possible NaN results.

Real-World Use

Numbers are used for prices, quantities, marks, percentages, ages, distances, scores, and calculations.

Practice Exercise

Practice: Numbers
Create a product price and quantity, calculate the total price, and display the result in the console.

Ready to write code?

Open the JavaScript editor and run your code with live output.

Try It Yourself →