JavaScript provides a set of built-in Number methods that allow you to work with numeric data effectively. These methods can be used to convert numbers, format them, and perform various numerical operations.
In this tutorial, we will explore several Number methods, explain their usage, and illustrate them with code examples.
Table of Contents
1. toString() Method
The toString() method converts a number to a string. You can optionally specify a base (radix) for the conversion, which is particularly useful for converting numbers to binary, octal, hexadecimal, etc.
Example 1: Using toString()
const number = 255; console.log(number.toString()); // Output: '255' console.log(number.toString(2)); // Output: '11111111' (binary) console.log(number.toString(8)); // Output: '377' (octal) console.log(number.toString(16)); // Output: 'ff' (hexadecimal)
Explanation: By default, toString() converts the number to a decimal string. Specifying a radix (like 2, 8, or 16) converts the number into binary, octal, or hexadecimal, respectively.
2. toFixed() Method
The toFixed() method formats a number to a specified number of decimal places. It returns a string representation of the number.
Example 2: Using toFixed()
const pi = 3.14159; console.log(pi.toFixed(2)); // Output: '3.14' console.log(pi.toFixed(4)); // Output: '3.1416' console.log(pi.toFixed(0)); // Output: '3'
Explanation: toFixed() rounds the number to the specified number of decimal places. Note that it returns a string, not a number.
3. toPrecision() Method
The toPrecision() method formats a number to a specified length, including both the integer and decimal parts. It returns a string.
Example 3: Using toPrecision()
const num = 123.456; console.log(num.toPrecision(4)); // Output: '123.5' console.log(num.toPrecision(6)); // Output: '123.456' console.log(num.toPrecision(2)); // Output: '1.2e+2'
Explanation: toPrecision() adjusts the number to the total number of significant digits. If the specified precision is less than the number of digits, the number is rounded and may be formatted using scientific notation.
4. toExponential() Method
The toExponential() method converts a number to an exponential notation. You can specify the number of digits after the decimal point.
Example 4: Using toExponential()
const largeNumber = 123456789; console.log(largeNumber.toExponential()); // Output: '1.23456789e+8' console.log(largeNumber.toExponential(2)); // Output: '1.23e+8' console.log(largeNumber.toExponential(4)); // Output: '1.2346e+8'
Explanation: toExponential() returns the number in exponential (scientific) notation as a string. The optional parameter specifies the number of digits after the decimal point.
5. Number.isInteger() Method
The Number.isInteger() method checks whether a value is an integer. It returns true if the value is an integer and false otherwise.
Example 5: Using Number.isInteger()
console.log(Number.isInteger(42)); // Output: true console.log(Number.isInteger(3.14)); // Output: false console.log(Number.isInteger('42')); // Output: false console.log(Number.isInteger(NaN)); // Output: false
Explanation: Number.isInteger() only returns true for values that are integers. Strings and special numeric values like NaN return false.
6. Number.isNaN() Method
The Number.isNaN() method determines if a value is NaN (Not-a-Number). It returns true if the value is NaN and false otherwise.
Example 6: Using Number.isNaN()
console.log(Number.isNaN(NaN)); // Output: true console.log(Number.isNaN(42)); // Output: false console.log(Number.isNaN('Hello')); // Output: false console.log(Number.isNaN(Number('Hello'))); // Output: true
Explanation: Number.isNaN() is different from the global isNaN() function because it does not try to convert the value to a number before checking if it is NaN.
7. Number.isFinite() Method
The Number.isFinite() method checks if a value is a finite number. It returns true for numbers that are neither Infinity, -Infinity, nor NaN.
Example 7: Using Number.isFinite()
console.log(Number.isFinite(100)); // Output: true console.log(Number.isFinite(Infinity)); // Output: false console.log(Number.isFinite(-Infinity)); // Output: false console.log(Number.isFinite(NaN)); // Output: false console.log(Number.isFinite('100')); // Output: false
Explanation: Number.isFinite() returns true only if the value is a finite number. Unlike the global isFinite() function, it does not convert strings to numbers.
8. Number.parseInt() and Number.parseFloat()
These methods are similar to the global parseInt() and parseFloat() functions. They convert a string to a number.
Example 8: Using Number.parseInt() and Number.parseFloat()
console.log(Number.parseInt('42')); // Output: 42 console.log(Number.parseInt('1010', 2)); // Output: 10 (binary to decimal) console.log(Number.parseFloat('3.14')); // Output: 3.14 console.log(Number.parseFloat('123abc')); // Output: 123
Explanation: Number.parseInt() parses a string and returns an integer. The second parameter in parseInt() specifies the base (radix) for the conversion. Number.parseFloat() parses a string and returns a floating-point number.
9. Number Properties
JavaScript provides several properties that can be used to get information about numbers:
Number.MAX_VALUE: The largest positive number that can be represented.
Number.MIN_VALUE: The smallest positive number that can be represented (close to zero).
Number.POSITIVE_INFINITY: Represents positive infinity.
Number.NEGATIVE_INFINITY: Represents negative infinity.
Number.NaN: Represents “Not-a-Number.”
Example 9: Using Number Properties
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308 console.log(Number.MIN_VALUE); // Output: 5e-324 console.log(Number.POSITIVE_INFINITY); // Output: Infinity console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity console.log(Number.NaN); // Output: NaN
Explanation: These properties provide information about the limits and special values of JavaScript numbers.
10. Number Conversion Methods
10.1 Number()
The Number() method converts a value to a number. If the value cannot be converted, it returns NaN.
Example 10: Using Number()
console.log(Number('42')); // Output: 42 console.log(Number('3.14')); // Output: 3.14 console.log(Number('Hello')); // Output: NaN console.log(Number(true)); // Output: 1 console.log(Number(false)); // Output: 0
Explanation: Number() tries to convert the given value to a number. It handles numeric strings, booleans, and more, but returns NaN for non-numeric strings.
Conclusion
JavaScript provides a variety of methods for handling and manipulating numbers.
From formatting numbers with toFixed() and toPrecision() to performing checks with Number.isNaN() and Number.isFinite(), these methods enhance how you can work with numeric data in your JavaScript programs.
Understanding these methods will help you handle numeric operations more efficiently and correctly. Feel free to experiment with the examples to get a solid grasp of these Number methods!