Chapter 10
JavaScript Numbers
Numbers are used for calculations and numeric data such as age, marks, prices, quantities, and percentages.
01 • Learn
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.
02 • Syntax
Syntax
let age = 20; let price = 499.99; let total = 100 + 50;
03 • Example
Example
Try understanding this code before running it.
let price = 500; let quantity = 3; let total = price * quantity; console.log(total);
04 • Remember
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.
05 • Avoid
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.
06 • Practical
Real-World Use
Numbers are used for prices, quantities, marks, percentages, ages, distances, scores, and calculations.
07 • Practice
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.