JavaScript Number Methods

JavaScript provides several built-in methods for the Number object, which allow you to perform various operations and conversions on numbers. These methods are helpful for formatting, converting, and validating numbers.

1. toString()

Converts a number to a string.

let num = 123;
let str = num.toString();
console.log(str);  // Output: "123"

Try It Now

Usage: Can be used to convert a number to a string representation in any base (2 to 36).

let num = 255;
console.log(num.toString(2));  // Output: "11111111" (binary)
console.log(num.toString(16)); // Output: "ff" (hexadecimal)

Try It Now

2. toFixed()

Formats a number to a specified number of decimal places.

let num = 5.6789;
console.log(num.toFixed(2));  // Output: "5.68"

Try It Now

Usage: Useful for controlling the precision of floating-point numbers.

3. toPrecision()

Formats a number to a specified total number of significant digits.

let num = 123.456;
console.log(num.toPrecision(4));  // Output: "123.5"

Try It Now

Usage: Can format a number to a specific length, either as a whole or with decimal places.

4. valueOf()

Returns the primitive value of a Number object.

let numObj = new Number(123);
console.log(numObj.valueOf());  // Output: 123

Try It Now

Usage: Converts Number objects to primitive number values.

5. parseInt()

Parses a string and returns an integer.

let num = parseInt('123.45');
console.log(num);  // Output: 123

Try It Now

Usage: Converts strings to integers, ignoring any decimal part.

6. parseFloat()

Parses a string and returns a floating-point number.

let num = parseFloat('123.45');
console.log(num);  // Output: 123.45

Try It Now

Usage: Converts strings to floating-point numbers, retaining decimal places.

7. Number()

Converts various types to a number.

console.log(Number('123'));    // Output: 123
console.log(Number('123.45')); // Output: 123.45
console.log(Number(true));     // Output: 1
console.log(Number(false));    // Output: 0

Try It Now

Usage: Converts strings, booleans, and other types to numbers.

8. isFinite()

Checks if a value is a finite number.

console.log(isFinite(123));     // Output: true
console.log(isFinite(Infinity)); // Output: false

Try It Now

Usage: Determines if a number is finite and not Infinity or NaN.

9. isNaN()

Checks if a value is NaN (Not-a-Number).

console.log(isNaN('Hello'));  // Output: true
console.log(isNaN(123));      // Output: false

Try It Now

Usage: Determines if a value is an invalid number.

10. Number.isInteger()

Checks if a value is an integer.

console.log(Number.isInteger(123));    // Output: true
console.log(Number.isInteger(123.45)); // Output: false

Try It Now

Usage: Useful for verifying that a value is a whole number.

11. Number.isSafeInteger()

Checks if a value is a safe integer (within the safe range for JavaScript numbers).

console.log(Number.isSafeInteger(9007199254740991));  // Output: true
console.log(Number.isSafeInteger(9007199254740992));  // Output: false

Try It Now

Usage: Ensures a number is within the range where precision is guaranteed (-2^53 to 2^53).

12. Number.toExponential()

Converts a number to exponential notation.

let num = 123456;
console.log(num.toExponential(2));  // Output: "1.23e+5"

Try It Now

Usage: Formats a number using exponential (scientific) notation.

13. Number.toLocaleString()

Returns a string with a language-sensitive representation of the number.

let num = 1234567.89;
console.log(num.toLocaleString());  // Output: "1,234,567.89" (in US English)

Try It Now

Usage: Formats a number according to the local language conventions.

Summary of JavaScript Number Methods

  • toString(), toFixed(), and toPrecision() help in formatting numbers.
  • parseInt(), parseFloat(), and Number() assist in converting strings and other data types to numbers.
  • isFinite() and isNaN() help in validating number values.

These methods provide a powerful set of tools for working with numbers in JavaScript, allowing you to perform various operations like conversion, formatting, and validation easily.