JavaScript Tutorial

Javascript Numbers

The JavaScript Number object is used to represent numeric values, including integers and floating-point numbers. Numeric values can be written with or without decimal places.

Here are some examples:

var x = 6.15;    // A number with decimals
var y = 6;       // A number without decimals

In addition to assigning numeric values directly, you can also create number objects using the Number() constructor:

var num = new Number(value);

JavaScript supports scientific notation for representing very large or very small numbers:

var x = 132e5;   // 12300000
var y = 132e-5;  // 0.00132

JavaScript Numbers are Always 64-bit Floating Point

It's important to note that JavaScript numbers are always represented as 64-bit floating-point numbers, following the IEEE 754 standard. This format allocates 64 bits of memory, with the fraction portion of the number stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63.

Value (aka Fraction/Mantissa)

Exponent

Sign

52 bits (0 - 51)

11 bits (52 - 62)

1 bit (63)

Note : If the value is not a number and can't be converted to a number then it returns NaN(Not a Number) that can be validated by isNaN() method.

Precision

In JavaScript, integers are accurate up to 15 digits. Numbers with more than 15 digits may not be represented accurately and may lose precision.

Here are a couple of examples:var x = 999999999999999;    // x will be 999999999999999
var y = 9999999999999999;   // y will be 10000000000000000

In the first example, the integer value is within the range of accurate representation, so it remains unchanged. In the second example, the number exceeds the precision limit, and it gets rounded up to the nearest representable value.

When it comes to floating-point numbers, the maximum number of decimals is 17. However, it's important to note that floating-point arithmetic in JavaScript may not always be 100% accurate due to the limitations of the underlying binary representation.

JavaScript Number Constants

JavaScript also provides several number constants for convenience. Here is a list of some commonly used 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.

Adding Numbers and Strings

When adding numbers in JavaScript using the + operator, the result will be the sum of the numbers:

var x = 20;
var y = 30;
var z = x + y; // z will be 50 (a number)


However, if you add two strings together, JavaScript will perform string concatenation:

var x = "20";
var y = "30";
var z = x + y; // z will be "2030" (a string)

Note: JavaScript uses the + operator for both addition and string concatenation. When the operands are numbers, they will be added. When the operands are strings, they will be concatenated.

Numeric Strings

JavaScript can also handle numeric content in strings. It will try to convert strings to numbers for numeric operations:

var x = "200";
var y = "10";
var z = x / y; // z will be 20

In this example, the strings "200" and "10" are converted to numbers before performing the division operation.

This will work:

NaN - Not a Number

If you attempt to perform arithmetic operations with non-numeric strings, the result will be NaN (Not a Number):

var x = 100 / "Apple"; // x will be NaN (Not a Number)

Infinity

JavaScript supports the special values Infinity and -Infinity. These values are returned when you calculate a number that is outside the range of the largest possible number:

var x = 1 / 0; // x will be Infinity

This occurs when dividing a non-zero number by zero.

Hexadecimal

JavaScript can interpret numeric constants as hexadecimal if they are preceded by "0x":

var x = 0xFF;        // x will be 255

By default, JavaScript displays numbers as base 10 decimals. However, you can use the toString() method to output numbers in different bases, from base 2 to base 36.

Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.

Here are some examples:

var myNum = 32;

myNum.toString(10); // returns "32" (base 10)
myNum.toString(32); // returns "10" (base 32)
myNum.toString(16); // returns "20" (base 16)
myNum.toString(8);  // returns "40" (base 8)
myNum.toString(2);  // returns "100000" (base 2)

In these examples, myNum.toString(base) converts myNum to a string representation in the specified base.

Numbers Can be Objects

JavaScript numbers can be primitive values created from literals, such as var x = 600;. However, numbers can also be defined as objects using the new keyword:

var y = new Number(600);

When using the == operator, equal numbers are considered equal:

var x = 600;
var y = new Number(600);
// (x == y) is true because x and y have equal values

However, when using the === operator, equal numbers are not considered equal because the === operator expects equality in both type and value:

var x = 600;
var y = new Number(600);
// (x === y) is false because x and y have different types

Even objects cannot be directly compared using the == or === operators:

var x = new Number(600);
var y = new Number(600);
// (x == y) is false because objects cannot be compared

It's important to note that creating Number objects is not recommended as it can slow down execution speed and complicate the code. It may also produce unexpected results. It's generally better to use primitive values for numbers instead of Number objects.

Go back to Previous Course