Home » JavaScript » JavaScript Numbers

JavaScript Numbers

JavaScript treats and represent all the numbers as floating-point numbers. It does not have any different data types defined for a number. The calculations in javascript are very tricky as it automatically converts the types and gives the result, it can sometimes lead to wrong outputs.

The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. It follows IEEE standard to represent the floating-point numbers. A number object can be created by the help of Number() constructor.

JavaScript supports both integer and floating-point numbers that can be represented in decimal, hexadecimal or octal notation. Unlike other languages, JavaScript does not treat integer and floating-point numbers differently. All numbers in JavaScript are represented as floating-point numbers. If the value can't be converted to number, it returns NaN(Not a Number) that can be checked by isNaN() method.

Syntax
var val = new Number(number);

Property Description
var x=100; Integer Value
var x=105.5; Floating point number
var x=13e4; Exponent value, output: 130000
var n=new Number(16); Integer value by number object

JavaScript Number Constants

Property Description
MIN_VALUE The smallest positive representable number - that is, the positive number closest to zero (without actually being zero).
MAX_VALUE The largest positive representable number
POSITIVE_INFINITY Represents positive infinity, returned on overflow.
NEGATIVE_INFINITY Represents negative infinity, returned on overflow.
Nan Represents "Not a Number" value.

JavaScript MIN_VALUE

The MIN_VALUE property gives the minimum positive value closest to zero. Numbers smaller than this are converted to zero.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript MIN_VALUE Number Constant </title>
</head>
<body>
<script>
var b = Number.MIN_VALUE;
document.write(b + "<br>"); // 5e-324
</script>
</body>
</html>

Output

JavaScript MIN_VALUE Number Constant Example

JavaScript MAX_VALUE

The MAX_VALUE property gives the maximum possible positive value in Javascript. Numbers larger than MAX_VALUE are treated as infinity.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript MAX_VALUE Number Constant </title>
</head>
<body>
<script>
var a = Number.MAX_VALUE;
document.write(a + "<br>"); // 1.7976931348623157e+308
var x = Number.MAX_VALUE * 2;
document.write(x + "<br>"); // Infinity
var y = -1 * Number.MAX_VALUE * 2;
document.write(y); // -Infinity
</script>
</body>
</html>

Output

JavaScript MAX_VALUE Number Constant Example

JavaScript POSITIVE_INFINITY

The POSITIVE_INFINITY property can be considered as a number which is greater than any other number.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript POSITIVE_INFINITY Constant </title>
</head>
<body>
<script>
var x = Number.POSITIVE_INFINITY;
document.write(x + "<br>"); // Infinity
</script>
</body>
</html>

Output

JavaScript POSITIVE_INFINITY Constant

JavaScript NEGATIVE_INFINITY

The NEGATIVE_INFINITY property can be considered as a number which is lower than any other number.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript NEGATIVE_INFINITY Constant </title>
</head>
<body>
<script>
var x = Number.NEGATIVE_INFINITY;
document.write(x + "<br>"); // Infinity
</script>
</body>
</html>

Output

JavaScript NEGATIVE_INFINITY Constant

JavaScript Infinity

JavaScript Infinity represents a number too big for JavaScript to handle. JavaScript has special keyword Infinity and -Infinity that represent positive and negative infinity respectively.

For example, dividing by 0 returns Infinity, as demonstrated below:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript INFINITY Constant </title>
</head>
<body>
<script>
var x = 10 / 0;
document.write(x + "<br>"); // Infinity
var y = -10 / 0;
document.write(y); // -Infinity
</script>
</body>
</html>

Output

JavaScript INFINITY Constant Example











Follow Us: