At work we are currently preparing setting up our environment using tools such as babel to allow us to work with the newest JavaScript features without worrying about browser support. So I am going to start taking a look at some of these new features and hopefully start incorporating them into my daily repertoire.
In this post I am going to take a look at the Number and Math features which are pretty easy, and mostly already implemented by modern browsers.
In this post I am going to take a look at the Number and Math features which are pretty easy, and mostly already implemented by modern browsers.
While Math can be scary, using these new features don't have to be. We will start with the Number features and then move into the Math ones.
Numbers:
1. Support for octal and binary notation. You can now specify numbers as octal (0o###), or binary (0b###); So 8 (0o10) + 2 (0b10) + 10 + 16 (0x10) = 36
2. Support for octal and binary strings. Since octal and binary formats are now supported you can pass those formats into the Number() function and it will convert it into an integer. In the fiddle below the variables are converted to numbers using the Number function and we obtain the same result.
3. Number.EPSILON is a new constant that represents the difference between the smallest possible number greater than 1 and 1. So essentially the smallest possible number that can exist greater than zero. It represents
4. Number.isInteger is a new function that lets you know if a number being passed in is an integer. If it is not an integer or not a number then it will return false.
5. Number.isSafeInteger is similar to the function above, but also checks to see if the number is "safe". Due to the way that numbers are represented in JS, there are max and minimum numbers that are "safe", and any numbers beyond that are potentially off. What do I mean by "off"? Essentially two or more integers beyond the max or minimum are represented by the same JavaScript integer. Check out the third line in the fiddle below for wtf I am talking about.
6. Math.sign will return -1, 1, 0 (or -0....), or NaN based on the value passed in.
7. Math.trunc will turn the number into an integer which will essentially round down if greater than 0 or round up if less than 0.
8. There are new trig methods such as Math.sinh, Math.cosh, Math.tanh, Math.asinh, Math.acosh, Math.atanh. There is also a new Math.hypot function that calculates the square root of the sum of the objects.
9. There are also some new exponential and root functions available. Math.cbrt returns the cube root of the number passed in. Math.expm1 and Math.logp1 are similar to the Math.exp and Math.log, but from what I can tell by reading online they are more accurate when it comes to numbers close to 1.
I believe that covers most if not all of the major changes in ECMA Script 6. If I forgot something let me know and I will make sure to update the post.
Cheers!
3. Number.EPSILON is a new constant that represents the difference between the smallest possible number greater than 1 and 1. So essentially the smallest possible number that can exist greater than zero. It represents
2.2204460492503130808472633361816E-16.
From what I can tell it is most useful in testing equality of decimals. So instead of doing .3 - .2 - .1 === 0 (false) you can do Math.abs(.3 - .2 - .1) <= Number.EPSILON4. Number.isInteger is a new function that lets you know if a number being passed in is an integer. If it is not an integer or not a number then it will return false.
5. Number.isSafeInteger is similar to the function above, but also checks to see if the number is "safe". Due to the way that numbers are represented in JS, there are max and minimum numbers that are "safe", and any numbers beyond that are potentially off. What do I mean by "off"? Essentially two or more integers beyond the max or minimum are represented by the same JavaScript integer. Check out the third line in the fiddle below for wtf I am talking about.
6. Math.sign will return -1, 1, 0 (or -0....), or NaN based on the value passed in.
7. Math.trunc will turn the number into an integer which will essentially round down if greater than 0 or round up if less than 0.
8. There are new trig methods such as Math.sinh, Math.cosh, Math.tanh, Math.asinh, Math.acosh, Math.atanh. There is also a new Math.hypot function that calculates the square root of the sum of the objects.
9. There are also some new exponential and root functions available. Math.cbrt returns the cube root of the number passed in. Math.expm1 and Math.logp1 are similar to the Math.exp and Math.log, but from what I can tell by reading online they are more accurate when it comes to numbers close to 1.
I believe that covers most if not all of the major changes in ECMA Script 6. If I forgot something let me know and I will make sure to update the post.
Cheers!
No comments:
Post a Comment