What You Should Know About Numbers in ES6 JavaScript

Written by jsborked | Published 2017/02/01
Tech Story Tags: javascript | programming | numbers | es6 | web-development

TLDRvia the TL;DR App

The datatype Number is used for various purposes in JavaScript. Starting with ES6, Number has a number of improvements to help the programmer save time as well as code efficiently. Here are some new features that ES6 brings to our favorite datatype.

Binary and Octal Literals

For starters, programmers can now use prefixes to represent the different number systems in ES6.

So for representing binary digits, the prefix 0b can be used while for representing octal numbers, the prefix 0o can be used.

Number.NaN

Number.NaN is different than its global counterpart. It does not cast it’s input through Number(value) before comparision.

Number.NaN returns if the provided value IS NaN.

The confusing thing is, typeof NaN === number

So, typeof value === number, does not accurately tell you if value is a number. It gives the set of all numbers AND NaN

So, a more accurate way to determine if value is a number is:typeof value === ‘number’ && !Number.isNaN(value)

Of course, other ways of working around values being incorrectly described as numbers include, Number(x) === x which will return true for numbers only.

Number.isFinite

Number.isFinite differs from the global isFinite as well, in that Number.isFinite does not cast values through Number(value) before evaluation.

Global isFinite will evaluate non-numbers as finite!This is because global isFinite is casting values to Number(value) before evaluation.

For instance, Non-numbers cast to numbers can equal 0. Number(null)===0

Number.isFinite is superior in this regard.

Number.isInteger

This is new in ES6, and will determine if a number is an integer. No casting to Number() is done, so you can be sure non-numbers like null will evaluate false.

Number.parseInt, Number.parseFloat

Number.parseInt and Number.parseFloat behave exactly as their global counterparts without exceptions.

Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER

These represent the largest and smallest numbers that can be safely and precisely represented in the JavaScript implementation, which is bounded by the IEEE floating point limit (IEEE 754) of 2⁵³-1

Of course, the MIN safe integer is the negative of the MAX number. Remember how computers store numbers in memory.

Number.isSafeInteger

Number.isSafeInteger simply returns if the provided number is between the MIN and MAX values above.

Number.EPSILON

Rounding errors occur in JavaScript when floating point (decimal) numbers cannot be represented accurately enough by the system. For instance, shockingly enough .1 plus .2 does not equal .3!

Number.EPSILON provides an acceptable margin of error for use in computations. So, can be used to determine an acceptable margin of error, like this:

Numbers in ES6 have come a long way and while there is still room for improvement, enhancements like this help to make ES6 better than previous JavaScript versions.


Published by HackerNoon on 2017/02/01