Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Ecma 1
  • Overview
  • Transcript

3.2 New Features of the Number Object

In this lesson we'll take a look at some of the new properties and methods that have been added to the built-in Number object.

3.2 New Features of the Number Object

Hi folks, in this lesson we're going to look at some of the enhancements that have been made to the built in number object in ES 6. As well as new methods we also have several new properties. Let's take a look at these first. Like in the last lesson, we'll run these examples directly in Chrome using the polyfill. We have three new properties, EPSILON, MAX_SAFE_INTEGER, AND MIN_SAFE_INTERGER. All three of these properties are static properties that we can access from the built in number objects. And all three are constants. EPSILON is the difference between 1 and the smallest number greater than 1. It's set to a very tiny number, indeed. The number that the EPSILON constant property returns is nought point nought, followed by 14 zeros. Then the part of the string before the e that we can see in the console here. This might be useful for telling whether two fractions are close enough to be considered equal. The other two properties are the maximum and minimum safe integers that can be used in JavaScript, which equate to 2 to the power of 53 minus 1 as the max, and the negative version of the same value for the minimum. Let's move on to look at some number methods now. We have three entirely new static methods on the number object, as well as three existing methods which have been updated for ES6. The new methods are isFinite, isInteger and isSafeInteger. The updated methods are isNan, parseInt and parseFloat. The new methods are all very simple and each accepts a single parameter. A value which will be tested for it's finiteness or integernist, safe or otherwise. So we call the isFinite method on the built-in Number Object and we pass it the number that we'd like to check whether it is finite or not. In this case, the number 1 is finite, so the method returned true. In the second case, the special number, infinity, is not finite, and so the method returns false. We can also check whether a number is an integer. In this case the number 1 is an integer and so the first log returns true. But the value 1.1 is not an integer, and so the method returns false in the second log. A safe integer is any integer in the range minus 2 to power of 53 minus 1 to plus 2 to power of 53 minus 1. So in this example, the value one is a safe integer. Whereas the value two raised to the power of 53 is not a safe integer. And so, again, we see true followed by false in the console. So, let's take a quick look at the methods that have been updated. We can now use isNaN as a static method of the number object instead of the Global.isNaN function, the new method is preferable, because unlike the Global.isNaN, it doesn't coerce the parameter to a number. The new method is more reliable at detecting actual NaN values. With the Global.isNaN, the string NaN is coerced to a number, and so this method would return true. The new method, however, returns false, as it should do. The new parseInt and parseFloat static methods behave in the same way as the global functions of the same name. They've simply been moved to static methods of the Number object for consistency with some of the other methods that we've looked at. So, in this lesson we learned about the new properties and methods that have been added to the Number object. We saw that all of them are static, meaning that we can use them on the Number object instead of individual Number instances. We saw that there are three new properties, EPSILON, MIN_SAFE_INTEGER, and MAX_SAFE_INTEGER. Three new methods, isFinite(), isInteger(), and isSafeInteger(). And also three updated methods, isNaN(), parseFloat(), and parseInt(). Thanks for watching.

Back to the top