JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Decimal Numbers
  • Binary Numbers
  • Octal Numbers
  • Hexadecimal Numbers
  • Exponentiation
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 6 Numbers and Dates
  4. Numbers

Numbers Enhanced

PreviousNumbersNextMath

Last updated 7 years ago

JavaScript implements all numbers in the . This basically means that JS incorporates all the numbers between -(253) -1 to 253 - 1. There is no specific type for numbers. JavaScript is both able to represent floating-point numbers, as well as the symbolic value of Infinity, -Infinity, and NaN (Not a Number). Basically, JS has in its library a vast supply of integers that are at the disposal of the developer.

You can use numberic literals for the following: decimal, binary, octal, and hexadecimal.

Decimal Numbers

Note: decimal literals can start with 0, followed by another decimal digit; however, if EVERY number following the 0 is LESS THAN 8, the number is parsed as an octal number.

1234567890
1150

// Use caution when starting a number with a 0

// Parsed as a decimal

0888 // 888

// Parsed as an octal

0777 // 511 - parsed as octal in non-strict mode

Binary Numbers

Binary number syntax incorporates a leading 0, followed by the letter 'B' (upper or lower case - b or B). The appropriate numbers following should be either 0 or 1. If the following numbers are not zero or one, a SyntaxError is thrown.

var a = 0b10000000000000000000000000000000; // 2147483648
var b = 0b01111111100000000000000000000000; // 2139095040
var c = 0B00000000011111111111111111111111; // 8388607

Octal Numbers

Octal number syntax begins with a 0, and is followed by digits ranging between 0 and 7.

var n = 0755; // 493
var m = 0644; // 420

Strict mode in ES2015 (or higher) forbids octal syntax; rather, if desired, the developer must prefix an octal number with a zero, followed by the letter 'O' (0o).

// for ES2015

var a = 0o10; // 8

Hexadecimal Numbers

Hexadecimal numbers incorporate alpha-numberic digits to convey specific numeric values. The syntax uses a 0, followed by the letter 'X' (upper or lower case - x or X). The range of the digits must be the following: 0123456789ABCDEF; if it is outside this range, a SyntaxError is thrown.

0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10

Exponentiation

1E3   // 1000
2e6   // 2000000
0.1e2 // 10
double-precision 64-bit binary format IEEE 754