JavaScript Number object represents a numerical date, either integers or floating-point numbers. The browser automatically converts number literals to instances of the number class.
The syntax for creating a number object
var val = new Number(number);
Number() – Returns a number, converted from its argument.
Number objects in JavaScript
Simple example code Number()
can be used to convert JavaScript variables to numbers:
<!DOCTYPE html>
<html>
<body>
<script>
console.log(Number(true))
console.log(Number("10"))
console.log(Number("John"))
console.log(Number(false))
</script>
</body>
</html>
Output:
Number()
can also convert a date to a number.
<script>
var num = Number(new Date("2022-01-01"))
console.log(Number(true))
</script>
Output: 1
Number Constants
Let’s see the list of JavaScript number constants with descriptions.
Constant | Description |
---|---|
MIN_VALUE | returns the largest minimum value. |
MAX_VALUE | returns the largest maximum value. |
POSITIVE_INFINITY | returns positive infinity, overflow value. |
NEGATIVE_INFINITY | returns negative infinity, overflow value. |
NaN | represents the “Not a Number” value. |
Number Methods
Let’s see the list of JavaScript number methods with their description.
Methods | Description |
---|---|
isFinite() | It determines whether the given value is a finite number. |
isInteger() | It determines whether the given value is an integer. |
parseFloat() | It converts the given string into a floating-point number. |
parseInt() | It converts the given string into an integer number. |
toExponential() | It returns the string that represents the exponential notation of the given number. |
toFixed() | It returns the string that represents a number with exact digits after a decimal point. |
toPrecision() | It returns the string representing a number of specified precision. |
toString() | It returns the given number in the form of a string. |
Comment if you have any doubts or suggestions on this JS object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version