JavaScript

# Working with Numbers in JavaScript | Explained with examples

In any programming, language numbers play a significant role; therefore, most of the programming languages defined different types of numbers. However, JavaScript has only one type of number “floating-point numbers” that follows the IEEE 754 standards.

JavaScript, always stored the numbers in the form of floating-point or double-precision, and it stored the numbers in 64 bits. In JavaScript numbers are also known as the fractions which are stored in bits from 0 to 51, the sign is stored in 63 bits and the exponent is stored in bits 52 to 62 bits.

In JavaScript a number can be used in the following forms:

## Numbers with or without Decimals

To write the numbers in JavaScirpt with or without decimal points i as follows:

let a = 25;

let b = 3.55;

## How to write extra-large or extra-small numbers in JavaScript

To write extra-large numbers in Javascript the following syntax will be used:

let c = 2776e3 // 276000

let d = 43e-2 // 0.0043

## Integer precision

As we discussed above, in JavaScript numbers are floating-point numbers; therefore, we should also know about the precision of an integer. In JavaScript, an integer is accurate up to 15 digits as shown in the given example.

Example

let a =999999999999999; // a will be equal to 999999999999999

let b =9999999999999999; // b will be equal to 100000000000000

## Floating Precision

Floating precision is also a part of floating-point numbers in JavaScript. However, when we apply some arithmetic operation on floating numbers, their answer will not be accurate. Have a look at the given example.

Example

let c = 0.7 + 0.2; // out will be 0.8999999999999999

This problem can be solved by applying the multiplication and division operations on it.

let c = (0.7*10 + 0.2*10) / 10

## Number’s working with string

In JavaScript if we add a number with a number in string, then instead of addition, concatenation takes place. As shown in the given example.

let a = 7;

let b = "45"

c = a+b;

However, if we apply other arithmetic operations on two strings then in resultant we will get numbers instead of a string as shown in the following example.

let a = "70";

let b = "40";

let c = a / b;

let d = a * b;

let e = a -b;

console.log(c); // output will be 1.75

console.log(d); // output will be 2800

console.log(e); // output will be 30

## Symbolic number values

The floating-point numbers further have three types of symbolic values:

• NaN (Not a Number)
• +Infinity Number
• -Infinity Number

## NaN (Not a Number)

In JavaScript, if the result of some arithmetic operation is not a number then NaN is returned as shown in the code snippet given below:

let a = 5/ 'cat'

Moreover, isNaN() is a global function available in javascript for checking if the value is a number or not, and by default its initial value is “Not-A-Number”. The current browsers do not support this function because it is a non-writable and non-configured function. The following program shows an example of isNaN().

Example

let x = 100 / "someString";

console.log(x);

isNaN(x);

## Infinity

When it comes to calculation numbers, javascript has a limit and we can’t more than the largest possible number(1.7976931348623157e+308). Now, any number above than the largest possible number in javascript would be considered as an Infinity.

Let’s divide a number with zero and check the result:

let x = 24/0;

console.log(x);

In Javascript, the type of “infinity” is number:

typeof(x);

## Negative Infinity(-Infinity)

Just like Infinity, any number below than the smallest possible number(5e-324) in javaScript would be considered as a Negative Infinity(-Infinity).

Let’s divide a number with zero and check the result:

let x = -24/0;

console.log(x);

## Numbers as Object()

In javaScript numbers can also be represented in the form of an object. We can define numbers as object by using the keyword “new”. Take a look at the given example.

let a = 432; // a is a number

let b = new Number(432); // b is a Number object

console.log(typeof(a));

console.log(typeof(b));

## Conclusion

JavaScript has only one type of number known as “floating-point numbers” that follows the IEEE 754 standards. In JavaScript numbers are also known as the fractions which are stored in bits from 0 to 51, the sign is stored in 63 bits and the exponent is stored in bits 52 to 62 bits. This post explains how numbers behave with the strings during arithmetic operations, and what are symbolic number values with the help of examples.

### About the author

#### Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}