JavaScript has only one type to represent numbers. The Number type represents both integer values and floating-point values. All numbers use double-precision 64-bit floating-point format (IEEE-754).

There are several numeric literals in several formats, to support different types of numbers.

## Integers

A decimal integer is written as a sequence of digits:

var i = 5; var i2 = 100; var i3 = 1523;

In addition to base 10, integers can be represented as hexadecimal (base 16) or octal (base 8) values.

An hexadecimal literal begins with 0x, followed by a sequence of hexadecimal digits (digits 0 through 9 and letters a through f).

Case in numbers is insensitive.

var i = 0x64; // 100 (base 10)

An octal literal begins with the digit 0, followed by a sequence of octal digits (digits from 0 to 7). It is not part of ECMAScript 5, however all browsers support it. In ECMAScript 2015 octal literals are supported if they are prefixed with 0o.

var i = 0123; // 83 (base 10) var i2 = 0o143; // 99 (base 10)

## Floating point literals

Floating point literals are used for real numbers. A real value consists of integer part of the number followed by decimal point and the fractional part of the number.

var f = 120.45;

There is also exponential notation, useful for very small or very large numbers. It consists of a real number followed by the letter e, optional plus or minus sign and followed by an integer exponent.

var f = 9.231e15; // 9.231 x 1015 var f2 = 3.87e-16; // 3.87 x 10-16

## Calculations

Arithmetic calculations in JavaScript are performed using arithmetic operators like + (addition), – (subtraction), * (multiplication), / (division), % (modulo – remainder after division).

More complex operations JavaScript supports through the Math object and its methods and properties.

var x = 1, y = 2, z = 3; var result = x + y * z; // 7 var a = 9; var b = Math.sqrt(a);

## Rounding errors

JavaScript floating point values have high precision but are not accurate in arithmetic calculations. The IEEE-754 format that JavaScript numbers use is a binary representation. This representation cannot exactly represent numbers. This causes rounding errors in arithmetic operations and the difference between the approximations expected as the same results.

var a = 0.1 + 0.7; console.log(a); // 0.7999999999999999 var b = 0.3 + 0.5; console.log(b); // 0.8

## Infinity

Infinity is a special value to represent numbers that cannot be represented by JavaScript numeric range. JavaScript defines the smallest number that can be represented in Number.MIN_VALUE and the largest number in Number.MAX_VALUE. If calculation results in a number outside the range, returned value is Infinity (positive infinity) or -Infinity (negative infinity) for negative numbers outside range.

## NaN

NaN is a special numeric value and means “Not a Number”. It is used to represent result of an arithmetic operation that has failed and cannot produce normal result. For example if an operand of arithmetical operation is string which cannot be converted to number, the result will be NaN.

var x = 10, y = '###'; var n = x * y; console.log(n); // NaN

The result of any operation involving NaN is NaN.

NaN is not equal to any other value involving NaN. To check if the value is not a number (NaN), the isNaN() function have to be used. It returns true if the value passed to the function cannot be converted to number, otherwise returns false.

var test = (NaN == NaN); console.log(test); // false var n = isNaN(NaN); // true var n2 = isNaN("abc"); // true var n3 = isNaN(3); // false

## Number object type

Number is a wrapper object type for work with numerical values.

Some properties of the Number are:

*Number.MAX_VALUE* – The maximum representable number

*Number.MIN_VALUE* – The minimum representable number

*Number.NaN* – Special Not a Number value

### Number methods

*Number.parseInt(string)* – parses a string passed as the argument and returns an integer.

*Number.parseFloat(string)* – parses a string passed as the argument and returns a floating point number.

*Number.isInteger(value)* – verifies whether the passed value is an integer.

*Number.isNaN(value)* – verifies whether the passed value is NaN.

*Number.isSafeInteger(value)* – checks whether the passed value is a safe integer. A safe integer can be exactly represented as an IEEE-754 number (integers from (253 – 1) to -(253 – 1)).

Number.prototype methods:

*numberObject.toFixed([digits])* – returns a string representing the number in fixed-point notation. The optional digits param specifies the number of digits after the decimal point.

Example:

var numberObject = 123.4567; console.log(numberObject.toFixed()); // 123 console.log(numberObject.toFixed(1)); // 123.5 console.log(numberObject.toFixed(2)); // 123. 46 console.log(numberObject.toFixed(3)); // 123. 457

*
numberObject.toPrecision([precision])* – returns a string representing the number with to the specified precision (number of significant digits).

Example:

var numberObject = 1.4567; console.log(numberObject.toPrecision()); // 1.4567 console.log(numberObject.toPrecision(1)); // 1 console.log(numberObject.toPrecision(2)); // 1.5 console.log(numberObject.toPrecision(3)); // 1.46

## Math object

Math is an object which properties and methods represent mathematical constants and functions.

Note that Math is an abject, not object type and not constructor function.

### Math properties

One of Math object properties is:

*Math.PI* – mathematical constant number pi.

### Math methods

Some of Math object functions are:

*Math.min(x1, x2, …, xn)* – returns the lowest number passed into it.

*Math.max(x1, x2, …, xn)* – returns the largest number passed into it.

*Math.floor(x)* – returns the greatest integer less than or equal to a given number.

*Math.ceil(x)* – returns the least integer greater than or equal to a given number.

*Math.abs(x)* – returns the absolute value of a given number.

*Math.sign(x)* – returns the sign of a given number, determining if the number is positive, negative or zero.

*Math.random()* – returns a random floating-point number between 0 and 1.

*Math.round(x)* – returns an integer that is closest to a given number after rounding it.

*Math.trunc(x)* – returns the integer part of a number, fractional digits are removed.

*Math.pow(base, exponent)* – returns power of the base to the exponent.

*Math.sqrt(x)* – returns the square root of a given number,

*Math.cbrt(x)* – returns the cube root of a given number,

*Math.log10(x)* – returns the base 10 logarithm of a given number,

*Math.sin(x)* – returns the sine of the angle specified in radians,

*Math.cos(x)* – returns the cosine of the angle specified in radians,

*Math.tan(x)* – returns the tangent of the angle specified in radians,

## Date object type

There is no separate data type for dates. To work with dates and times you can use Date object type. Instance of Date represents single moment in time.

### Creating Date object

You create Date object using Date() constructor. There are several options of parameters that you can passed to it:

*new Date()*

*new Date(milliseconds)*

*new Date(dateString)*

*new Date(year, month, day, hours, minutes, seconds, milliseconds)*

The Date() constructor with no arguments creates Date object set to current date.

When one numeric argument is passed it is considered as representation of the date in milliseconds.

When one string argument is passed it is string representation of a date.

When to the constructor are passed between two and seven numeric arguments, they specify components of date and time.

### Date instance methods

Date instance methods inherit from Date.prototype.

getDate() – returns the day of the month as a number (1 – 31) of the specified date.

getDay() – returns the day of the week (0 – 6) for the specified date.

getMonth() – returns the month (0 – 11) for the specified date.

getFullYear() – returns the year (YYYY) of the specified date.

getHours() – returns the hour (1 – 23) in the specified date.

getMinutes() – returns the minute (0 – 59) in the specified date.

getSeconds() – returns the second (0 – 59) in the specified date.

getMilliseconds() – returns the millisecond (0 – 999) in the specified date.

getTime() – returns time in milliseconds since January 1, 1970.

ArtemMon Oct 25, 2021 at 1:38 pm

There’s a small mistake in this:

“getHours() – returns the hour (1 – 23) in the specified date.”

It actually returns the hour (0 – 23).