English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Numbers (Number)

JavaScript has only one number type, without separate specification for integers and floating-point numbers.

Therefore, numbers can be written in JavaScript, with or without decimals.

var a = 25; // Without decimal point
var b = 80.05;  // With decimal point
Test to see‹/›

In both of the above cases, the data type is a number, and it is the same regardless of whether the number has a decimal point or not.

Scientific notation can be used in JavaScript to abbreviate very large or very small numbers, as shown in the following examples:

let num1 = 957e8; // 95700000000
let num2 = 957e-8;// 0.00000957
Test to see‹/›

JavaScript numbers are considered accurate, up to15bit digits. This means that the number will be rounded to the16bits after rounding:

let num1 = 999999999999999; // The value remains unchanged 999999999999999
let num2 = 9999999999999999;// Rounded up to 10000000000000000
Test to see‹/›

Internal representation of numbers

Unlike other languages, JavaScript does not define different types of numbers, such as integers, short integers, long integers, floating-point numbers, etc.

JavaScript numbers have64Bit precision, also known as double precision.

The internal representation is based on IEEE 754Standard.

64The bits are allocated between the sign, exponent, and decimal part of the number, as shown below:

IdentifierExponentFraction
1 bit11 bit52 bit
Bit 63Bits 62–52Bits 51–0

Adding numbers and concatenating strings

From the previous chapters, we can know that,+Operators are used for adding numbers and concatenating strings.

If it is a number, add the numbers. If it is a string, concatenate the strings.

If two numbers are added together, the result will be a number:

let x = 50;
let y = 10;
let z = x + y;  // z  = 60 (a number)
Test to see‹/›

If two strings are concatenated, the result will be a string concatenation:

let x = "50";
let y = "10";
let z = x + y;  // z  = 5010 (a string)
Test to see‹/›

If a number and a string are concatenated, the result will be a string concatenation:

let x = 50;
let y = "10";
let z = x + y;  // z  = 5010 (a string)
Test to see‹/›

If a string and a number are concatenated, the result will be a string concatenation:

let x = "50";
let y = 10;
let z = x + y;  // z  = 5010 (a string)
Test to see‹/›

JavaScript evaluates expressions from left to right. Different sequences can produce different results.

In the following example, first convert50 + 10added together, because x and y are both numbers, and then60 +"30” concatenated together, because z is a string:

let x = 50;
let y = 10;
let z = "30";
let sum = x + y + z;  // sum  = 6030 (a string)
Test to see‹/›

A common mistake, expecting this result to be60:

let x = 50;
let y = 10;
let z = "The sum is: " + x + y;
Test to see‹/›

You can use parentheses to solve the above problem:

let x = 50;
let y = 10;
let z = "The sum is: " + (x + y);
Test to see‹/›

Numeric strings and mathematical expressions

JavaScript strings can contain numeric values.

  let x = 50;  // x is a number
  let y = "50";  // y is a string

If a string contains a numeric value, a mathematical expression can be executed in JavaScript.

Division can be performed:

let x = "50" / "10";  // x  = 5
Test to see‹/›

Multiplication can be performed:

let x = "50" * "10";  // x  = 500
Test to see‹/›

Exponentiation can be performed:

let x = "5" ** "3";  // x  = 125
Test to see‹/›

Increment can be performed:

let x = "50";
x++; // x  = 51
Test to see‹/›

Subtraction can be performed:

let x = "50" - "10";  // x  = 40
Test to see‹/›

Addition cannot be performed:

let x = "50" + "10";  // x  = 5010
Test to see‹/›

Note:If two strings are added together, the result will be a string concatenation.

JavaScript NaN-Invalid number

In JavaScript NaN, the reserved word indicates that the number is not a valid number.

If you try to perform mathematical operations on a number and a non-numeric value, NaN will be returned.

var x = 50 / "oldtoolbag.com";  // x  =  NaN (Not a Number)
Test to see‹/›

But if the string contains a numeric value, you can use JavaScript to evaluate mathematical expressions:

var x = 50 / "10";  // x  = 5
Test to see‹/›

You can use the global JavaScript isNaN()function to determine if a value is a number:

var x = 50 / "oldtoolbag.com";
isNaN(x);// returns true because x is not a number
Test to see‹/›

When assigning a value to the variable NaN used in an NaN operation, even if the other operand is a valid number, it will lead to the value:

var x = NaN;
var y = 27;
var z = x + y;  // z  =  NaN
Test to see‹/›

NaN is a number; typeof NaN returns number:

typeof NaN;   // will return "number"
Test to see‹/›

JavaScript Infinity

Infinity (positive infinity) or-Infinity (negative infinity) will be returned if the calculated number exceeds the maximum number available in JavaScript.

For undefined values, the same situations will occur, for example when dividing by zero:

var x = 5 / 0;   // will return infinity
var y = -5 / 0;  // will return negative infinity
Test to see‹/›

Technically speaking, Infinity (positive infinity) is displayed when the number exceeds the number1.797693134862315E+308,it represents the upper limit in JavaScript.

Similarly,-Infinity (negative infinity) is displayed when the number exceeds the lower limit-1.797693134862316E+308.

Infinity is a number: typeof Infinity returns number:

typeof Infinity;   // will return "number"
Test to see‹/›

The number Infinity can also be used in a loop:

var num = 5;
while (num != Infinity) { 
// The code here will execute until num = ∞
}
Test to see‹/›

JavaScript Radix

By default, JavaScript displays numbers as prefixed with10isof the basedecimal.

numbers can also be represented in hexadecimal(base16),binary(base2)and octal(base8)indicates.

The prefix for hexadecimal numbers is 0x:

var x = 0xFF;  // x  = 255
Test to see‹/›

The prefix for binary numbers is 0b:

var x = 0b1111;// x  = 15
Test to see‹/›

Do not write a number that starts with a zero (for example 0121If a number is written with a leading zero, JavaScript will interpret the number as octal:

var x = 016;
Test to see‹/›

You can use thistoString()method from2to36the number returns a number.

hexadecimal forRadix16decimal forRadix10octal forRadix8binary forRadix2.

var num = 255;
num.toString(10);  // returns 255
num.toString(16);  // returns ff
num.toString(8);   // returns 377
num.toString(2);   // returns 11111111
Test to see‹/›

Number primitives and number objects

通常,JavaScript数字是从文字创建的原始值:

var num = 50;

But, you can also use the new keyword to define a number as an object:

var num = new Number(50);

To test the difference between the two, we will initialize a number primitive and a number object.

var num1 = 50;
var num2 = new Number(50);
typeof num1// returns number
typeof num2// returns object
Test to see‹/›

Note:Do not create numbers as objects. This will slow down execution and produce some unexpected results.

When using the == operator, numbers are equal:

var num1 = 50;
var num2 = new Number(50);
document.write(num1 == num2); // returns true because num1and num2They have equal values
Test to see‹/›

When using the === operator, equal numbers are not equal because the === operator expects both value and type to be equal:

var num1 = 50;
var num2 = new Number(50);
document.write(num1 === num2); // returns false because num1and num2They have different types
Test to see‹/›

Objects cannot be compared:

var num1 = new Number(50);
var num2 = new Number(50);
document.write(num1 == num2); // returns false because num1and num2They are different objects
document.write(num1 === num2); // returns false because num1and num2They are different objects
Test to see‹/›

Note the difference between (==) and (===). Comparing two JavaScript objects will always return false.