JavaScript Data Type: Number
1 Introduction
The Number data type in JavaScript represents both integer and floating-point numbers
using a 64-bit floating-point format. It is a primitive type used for numerical values and
supports arithmetic operations.
2 Characteristics
• Types: Includes integers (e.g., 42), floating-point numbers (e.g., 3.14), and special
values like Infinity, -Infinity, and NaN (Not a Number).
• Arithmetic: Supports operators like +, -, *, /, and %.
• Precision: Limited by 64-bit floating-point representation, which may cause pre-
cision issues with very large or small numbers.
3 Use Cases
Numbers are used for:
• Performing calculations, such as financial computations or measurements.
• Managing counters or indices in loops.
• Handling numerical data in algorithms or data processing.
4 Example
Below is a JavaScript code example demonstrating the Number data type:
1 let age = 25; // Integer
2 let price = 19.99; // Floating - point
3 let infinity = Infinity ; // Special value
4 let notANumber = NaN ; // Invalid math result
5 console . log ( age + price ) ; // Output : 44.99
6 console . log (10 / 0) ; // Output : Infinity
7 console . log ( isNaN ( notANumber ) ) ; // Output : true
8 console . log ( Math . round ( price ) ) ; // Output : 20
1
5 Notes
• Use isNaN() to check for NaN.
• For large integers beyond safe limits, consider using BigInt (a separate type).
• Be cautious with floating-point arithmetic due to potential precision errors (e.g.,
0.1 + 0.2 may not equal 0.3 exactly).