Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Numbers

JS Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals.

					 
        
          let a = 10.76;  // A number with decimals
          let b = 10;     // A number without decimals
        
      
Extra Large Numbers

Extra large or extra small numbers can be written with scientific (exponent) notation

					 
        
          let a = 123e5;  // 12300000
          let b = 123e-5; // 0.00123
        
      
Number Representation

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

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

This format stores numbers in 64 bits, where the number (the fraction) is 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)
Integer Precision

Integers (numbers without a period or exponent notation) are accurate up to 15 digits. The maximum number of decimals is 17.

					 
        
          let a = 999999999999999;  // x will be 999999999999999
          let b = 9999999999999999; // y will be 10000000000000000
        
      
Floating Precision

Floating point arithmetic is not always 100% accurate:

					 
        
          let a = 0.2 + 0.1; // a = 0.30000000000000004

          // To solve the problem above, it helps to multiply and divide:
          let a = (0.2 * 10 + 0.1 * 10) / 10; // a = 0.3
        
      
Adding Numbers and Strings

JavaScript uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.

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

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

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

					 
        
          let a = 10;
          let b = 20;
          let c = a + b; // c = 30

          let k = "10";
          let l = "20";
          let m = k + l; // m = 1020

          let x = a + l; // m = 1020

          // Misleading Behavior
          let y = "The output of a + b: " + a + b; // y = The output of a + b: 1020
          let z = a + b + k; // m = The output of a + b: 3010
        
      
Type Conversion

JavaScript will try to convert strings to numbers in all numeric operations except addition (concatenation).

					 
        
          let a = "100";
          let b = "10";

          // Divide
          let c = a / b;  // c = 10

          // Multiply
          let c = a * b;  // c = 1000

          // Subtraction
          let c = a - b;  // c = 90

          // Subtraction
          let c = a + b;  // c = 10010
        
      
NaN - Not a Number

NaN is a JavaScript reserved word indicating that a number is not a legal number. Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number).

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

					 
        
          let a = 100 / "Apple";  // a = NaN

          isNaN(a);  // returns false

          typeof NaN;  // returns numbers
        
      
Infinity

Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.

					 
        
          let myNumber = 2;
          // Execute until Infinity
          while (myNumber != Infinity) {
            myNumber = myNumber * myNumber;
          }

          let x =  2 / 0;  // returns Infinity
          let y = -2 / 0;  // returns -Infinity

          typeof Infinity; // returns number
        
      
JavaScript Numbers as Objects

Normally JavaScript numbers are primitive values created from literals. But numbers can also be defined as objects with the keyword new.

					 
        
          let a = new Number(123);
        
      
Do not create Number objects. The new keyword complicates the code and slows down execution speed. Number Objects can produce unexpected results. Comparing two JavaScript objects always returns false.
					 
        
          let a = 100;
          let b = new Number(100);
          let c = new Number(100);

          (a == b)  // returns true
          (a === b)  // returns false
          (b == c)  // returns false
          (b === c)  // returns false
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved