Skip to content

Number object in JavaScript | Basic

  • by

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 object in JavaScript

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.

ConstantDescription
MIN_VALUEreturns the largest minimum value.
MAX_VALUEreturns the largest maximum value.
POSITIVE_INFINITYreturns positive infinity, overflow value.
NEGATIVE_INFINITYreturns negative infinity, overflow value.
NaNrepresents the “Not a Number” value.

Number Methods

Let’s see the list of JavaScript number methods with their description.

MethodsDescription
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

Leave a Reply

Your email address will not be published. Required fields are marked *