JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Numbers

JavaScript has only one number type, there is no separate designation for integers and floating-point numbers.

Because of this, numbers can be written in JavaScript with or without decimals.

Run code

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

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

Run code

Numbers in JavaScript are considered to be accurate up to 15 digits. That means that numbers will be rounded after the 16th digit is reached:

Run code

The Internal Representation of Numbers

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

JavaScript numbers have 64-bit precision, which is also called double precision.

The internal representation is based on the IEEE 754 standard.

The 64 bits are distributed between a number’s sign, exponent, and fraction as follows:

Sign Exponent Fraction
1 bit 11 bits 52 bits
Bit 63 Bits 62–52 Bits 51–0

Adding Numbers and Strings

As you know from the previous chapters, the + operator is used for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

Run code

If you add two strings, the result will be a string concatenation:

Run code

If you add a number and a string, the result will be a string concatenation:

Run code

If you add a string and a number, the result will be a string concatenation:

Run code

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

In the below example, first 50 + 10 is added because x and y are both numbers, then 60 + "30" is concatenated because z is a string:

Run code

A common mistake is to expect this result to be 60:

Run code

You can solve above problem by using parentheses:

Run code

Numeric Strings and Mathematical Expressions

JavaScript strings can contain numeric values.

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

Division can be performed:

Run code

Multiplication can be performed:

Run code

Exponentiation can be performed:

Run code

Incrementation can be performed:

Run code

Subtraction can be performed:

Run code

Addition cannot be performed:

Run code

Note: If you add two strings, the result will be a string concatenation.



JavaScript NaN - Not a Number

In JavaScript NaN is a reserved word indicating that a number is not a legal number.

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

Run code

However, if a string contains a numeric value, the mathematical expression can be performed in JavaScript:

Run code

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

Run code

When assigning the value NaN to a variable used in an operation, it will result in the value of NaN, even when the other operand is a legal number:

Run code

NaN is a number; typeof NaN returns number:

Run code

JavaScript Infinity

Infinity or -Infinity will be returned if you calculate a number outside of the largest possible number available in JavaScript.

These will also occur for values that are undefined, as when dividing by zero:

Run code

In technical terms, Infinity will be displayed when a number exceeds the number 1.797693134862315E+308, which represents the upper limit in JavaScript.

Similarly, -Infinity will be displayed when a number goes beyond the lower limit of -1.797693134862316E+308.

Infinity is a number: typeof Infinity returns number:

Run code

The number Infinity can also be used in loops:

Run code

JavaScript Number Base

By default, JavaScript displays numbers as base 10 decimals.

Numbers can also be represented in hexadecimal (base 16), binary (base 2), and octal (base 8) notation.

Hexadecimal numbers are prefixed with 0x:

Run code

Binary numbers are prefixed with 0b:

Run code

Never write a number with a leading zero (like 0121). JavaScript interpret numbers as octal if they are written with a leading zero:

Run code

You can use the toString() method to return numbers from base 2 to base 36.

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

Run code

Number Primitives and Number Objects

Normally, JavaScript numbers are primitive values, created from literals:

var num = 50;

But numbers can also be defined as objects using the new keyword:

var num = new Number(50);

In order to test the difference between the two, we will initialize a number primitive and a number object.

Run code

Note: Don't create numbers as objects. It slows down execution speed and can produce some unexpected results.

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

Run code

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

Run code

Objects cannot be compared:

Run code

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