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:
Number Constants
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 “Not a Number” value. |
Number Methods
Method | Description |
---|---|
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 integer | returns 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