In JavaScript, numbers are a data type that represent both integers and floating-point values. JavaScript numbers are always stored as double-precision floating-point numbers.
1. Number Types
- Integers: Whole numbers, positive or negative.
let intNum = 42;
- Floating-point Numbers: Numbers with decimals.
let floatNum = 3.14;
- Exponential Notation: Large or small numbers can be represented using exponential notation.
let expNum = 5e6; // 5000000
2. Number Properties
MAX_VALUE
: Largest possible number in JavaScript.console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
MIN_VALUE
: Smallest possible positive number in JavaScript.console.log(Number.MIN_VALUE); // Output: 5e-324
NaN
: Represents a value that is “Not-a-Number”.let result = 0 / 0; console.log(result); // Output: NaN
Infinity
: Represents infinity, which is greater than any number.console.log(1 / 0); // Output: Infinity
-Infinity
: Represents negative infinity.console.log(-1 / 0); // Output: -Infinity
3. Number Methods
toString()
: Converts a number to a string.let num = 123; console.log(num.toString()); // Output: "123"
toFixed()
: Formats a number to a specified number of decimal places.let num = 5.6789; console.log(num.toFixed(2)); // Output: "5.68"
toPrecision()
: Formats a number to a specified length.let num = 5.6789; console.log(num.toPrecision(3)); // Output: "5.68"
valueOf()
: Returns the primitive value of a number.let num = new Number(123); console.log(num.valueOf()); // Output: 123
4. Number Conversion
parseInt()
: Parses a string and returns an integer.let num = parseInt('123.45'); console.log(num); // Output: 123
parseFloat()
: Parses a string and returns a floating-point number.let num = parseFloat('123.45'); console.log(num); // Output: 123.45
Number()
: Converts a value to a number.let num = Number('123.45'); console.log(num); // Output: 123.45
5. Checking for Numbers
isNaN()
: Checks if a value is NaN.console.log(isNaN('Hello')); // Output: true
isFinite()
: Checks if a value is a finite number.console.log(isFinite(10 / 2)); // Output: true
Number.isInteger()
: Checks if a value is an integer.console.log(Number.isInteger(42)); // Output: true
6. Special Number Values
NaN
: Represents a computational error or an undefined mathematical result.console.log(0 / 0); // Output: NaN
Infinity
and-Infinity
: Result from division by zero or a number too large for JavaScript to handle.console.log(1 / 0); // Output: Infinity console.log(-1 / 0); // Output: -Infinity
7. Example: Basic Arithmetic Operations
let a = 10; let b = 3; console.log(a + b); // Output: 13 console.log(a - b); // Output: 7 console.log(a * b); // Output: 30 console.log(a / b); // Output: 3.3333 console.log(a % b); // Output: 1
Summary
- Numbers in JavaScript can represent both integers and floating-point values.
- Methods like
toFixed()
,parseInt()
, andNumber()
help in converting and formatting numbers. - Special values like
NaN
andInfinity
handle exceptional cases in numerical computations.
These capabilities make JavaScript numbers versatile for a wide range of mathematical operations and data manipulation tasks.