JavaScript Numbers

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

Example

var x = 123e5;    // 12300000
var y = 123e-5;   // 0.00123

Code#00:

Output:

JavaScript Numbers are Always 64-bit Floating Point

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:

This format stores numbers in 64 bits,

  • 0-51 bit stores value(fraction)
  • 52-62 bit stores exponent
  • 63-bit stores sign

The types of number literals You can use decimal, binary, octal, and hexadecimal.

Decimal Numbers:
JavaScript Numbers does not have different types of numbers(ex: int, float, long, short) which other programming languages do. It has only one type of number and it can hold both with or without decimal values.

Example

var x = 3.14;    // A number with decimals
var y = 3;       // A number without decimals

Code#01:

Output:

Binary Numbers
They start with 0b or 0B followed by 0’s and 1’s.

var x = 0b11; // x will be 3
var x = 0B0111; // x will be 7

Code#02:

Output:

Octal Numbers
They start with 0 followed by a number of ranges from 0-7. If any number is used it will be taken as a decimal number.

var x = 0101; //x will be 65
var x = 07124; //x will be 3668

Code#03:

Output:

Hexadecimal Numbers
They start with 0x or 0X followed by any digit belonging (0123456789ABCDEF)

var x = 0xfed; //x will be 4077

Code#03:

Output:

 

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin

Leave a Comment