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, returningNaN
if it cannot be converted.
2. Static Methods
Number.isFinite(value)
: Determines whether the passed value is a finite number.Number.isInteger(value)
: Checks if the value is an integer.Number.isNaN(value)
: Determines whether the value isNaN
. Note that this is different from the globalisNaN()
function.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.Number.parseFloat(string)
: Parses a string argument and returns a floating-point number.Number.parseInt(string, radix)
: Parses a string argument and returns an integer of the specified radix (base).Number.MAX_VALUE
: A constant representing the largest positive finite value.Number.MIN_VALUE
: A constant representing the smallest positive value greater than 0.Number.NEGATIVE_INFINITY
: A constant representing negative infinity.Number.POSITIVE_INFINITY
: A constant representing positive infinity.
3. Instance Methods
num.toExponential([fractionDigits])
: Returns a string representing the number in exponential notation.num.toFixed([digits])
: Formats a number using fixed-point notation, rounding if necessary.num.toPrecision([digits])
: Returns a string representing the number to a specified precision.num.toString([radix])
: Converts a number to a string in the specified base (radix).num.valueOf()
: Returns the primitive value of aNumber
object.
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.