Skip to content

JavaScript Number Object | Basics

  • by

JavaScript Number is a primitive data type that stores numeric values. In JavaScript don’t need to declare for integer or floating values using int, float, etc. JavaScript number object follows IEEE standards to represent the floating-point numbers.

    var x = 100; //integer value  
    var y = 102.7; //floating point value  
    var z = 13e4; //exponent value, output: 130000  
    var n = new Number(50); //integer value by number object  

With the help of the Number() constructor, you can create a number object in JavaScript.

new Number(value);

JavaScript number examples

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    const a = 3;
    const b = 3.13;

    console.log(a - b);

    console.log(typeof(a));
    console.log(typeof(b));
  </script>
</body>
</html> 

Output:

JavaScript Number Object

Number Constants

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 “Not a Number” value.

Number Methods

MethodDescription
isNaN()determines whether the passed value is NaN
isFinite()determines whether the passed value is a finite number
isInteger()determines whether the passed value is an integer
isSafeInteger()determines whether the passed value is a safe integer
parseFloat(string)converts the numeric floating string to a floating-point number
parseInt(string, [radix])returns a string with a language-sensitive representation of a number
converts the numeric string to an integerreturns a string value for a number in exponential notation
toFixed(digits)returns a string value for a number in fixed-point notation
toPrecision()returns a string value for a number to a specified precision
toString([radix])returns a string value in a specified radix(base)
valueof()returns the value of the number
toLocaleString()returns a string with a language sensitive representation of a number

Do comment if you have any doubts or suggestions on this JS Number 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading