JavaScript Number(value) function


In JavaScript, the Number object provides a set of methods and properties that allow you to work with numeric values. Here’s an overview of the most commonly used functions and properties associated with the Number object:

1. Number Constructors

  • Number(value): Converts the argument to a number, returning NaN if it cannot be converted.

    let num = Number("123"); // 123 let invalidNum = Number("abc"); // NaN

2. Static Methods

  • Number.isFinite(value): Determines whether the passed value is a finite number.

    console.log(Number.isFinite(123)); // true console.log(Number.isFinite(Infinity)); // false
  • Number.isInteger(value): Checks if the value is an integer.

    console.log(Number.isInteger(10)); // true console.log(Number.isInteger(10.5)); // false
  • Number.isNaN(value): Determines whether the value is NaN. Note that this is different from the global isNaN() function.

    console.log(Number.isNaN(NaN)); // true console.log(Number.isNaN("abc")); // false
  • Number.isSafeInteger(value): Checks if the value is a safe integer, meaning it is an integer within the safe range of -2^53 + 1 to 2^53 - 1.

    console.log(Number.isSafeInteger(9007199254740991)); // true console.log(Number.isSafeInteger(9007199254740992)); // false
  • Number.parseFloat(string): Parses a string argument and returns a floating-point number.

    console.log(Number.parseFloat("3.14")); // 3.14
  • Number.parseInt(string, radix): Parses a string argument and returns an integer of the specified radix (base).

    console.log(Number.parseInt("10", 2)); // 2 (binary) console.log(Number.parseInt("10.5")); // 10
  • Number.MAX_VALUE: A constant representing the largest positive finite value.

    console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
  • Number.MIN_VALUE: A constant representing the smallest positive value greater than 0.

    console.log(Number.MIN_VALUE); // 5e-324
  • Number.NEGATIVE_INFINITY: A constant representing negative infinity.

    console.log(Number.NEGATIVE_INFINITY); // -Infinity
  • Number.POSITIVE_INFINITY: A constant representing positive infinity.

    console.log(Number.POSITIVE_INFINITY); // Infinity

3. Instance Methods

  • num.toExponential([fractionDigits]): Returns a string representing the number in exponential notation.

    let num = 123456; console.log(num.toExponential(2)); // "1.23e+5"
  • num.toFixed([digits]): Formats a number using fixed-point notation, rounding if necessary.

    let num = 2.34567; console.log(num.toFixed(2)); // "2.35"
  • num.toPrecision([digits]): Returns a string representing the number to a specified precision.

    let num = 123.456; console.log(num.toPrecision(4)); // "123.5"
  • num.toString([radix]): Converts a number to a string in the specified base (radix).

    let num = 255; console.log(num.toString(16)); // "ff" (hexadecimal)
  • num.valueOf(): Returns the primitive value of a Number object.

    let numObj = new Number(42); console.log(numObj.valueOf()); // 42

Summary

JavaScript's Number object provides various methods and properties for working with numbers. These include type conversion, parsing, checking conditions (like whether a number is finite, integer, or safe), and formatting numbers into strings. Understanding these functions allows for effective manipulation and validation of numerical data in JavaScript applications.