If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
One of the harder, often misunderstood primitives within JavaScript is a number data type. So in this tutorial, not only will we look at the basics of number, but also its limitations, some special numbers, fancy modern syntax and lots, lots more. So by the end you will have complete mastery of this critical data type. So let's go. The number TE type within JavaScript is a 64 bit signed floating value. This means that it can be used to represent intes as well as decimals, as well as inactive numbers. Now make no mistake, they are the same TE type. So 1, 3, 3 7 will be exactly equal to 1 3 3 7 0.0000 if
00:36
you wanted to do that comparison, modern JavaScript numbers can also include an underscore, and this is purely for readability purposes. This does not impact the value that is contained by that number. Additionally, you can declare numbers with the scientific e notation. After the E, you are basically specifying how many times you want the decimal to move towards the right here. Of course, we are saying that starting with the value of A one, we want the decimal to move six times towards the right, giving us one, followed by six zeros, which is a million. We can actually provide a negative value after E as well,
01:10
and that will move the decimal point towards the left. So here we are saying that starting with one, move it six times towards the left, giving us 0.5 zeros and one similar to other primitive types. The type of operator also works with numbers and when you apply to a number value, it returns the string number. Now the number that type within JavaScript is a true boundary floating point value, which means that they suffer from the boundary floating point error that you might be familiar with in other programming languages. If by any chance this is the first time that you're experiencing this, you can actually visit this website to learn more about this fact, but in short,
01:46
it exists in most programming languages including Java c and c plus plus. As you would expect, JavaScript numbers support all the common mathematical operators. You can use the plus operator to add to numbers. For example, one plus one is two. You can use the minus operator to subtract one value from another. For example, five minus one is four. You can use star for multiplication. For example, five star two is 10. You can use the forward slash for division. For example, 10 divided by four gives us 2.5. You can use the person sign for remainder, also called modular.
02:20
So three divided by two gives us a remainder of one and six is a perfect factor of three, so that gives us a remainder of zero. JavaScript also has the exponent operator, which you might be familiar with as power and it is represented by double stars. For example, two to the power three gives us eight. Now with these mathematical operators there is also a concept of shorthand assignment. As an example, consider a simple variable, which we have initialized 2, 1, 2, 3. We can do a mathematical operation, for example, plus one and then assign the value back to the original variable. Now of course this does work because 1, 2, 3 plus one is 1, 2, 4. However,
02:58
there Is a shorter hand syntax for this particular statement. We can do the same thing with plus equal one, and what this does is actually takes the variable adds one to it and assigns it back to the variable. As far as the runtime behavior is concerned, these two statements are perfectly equivalent. And of course this shorthand version exists for all the other mathematical operators that we've seen as well. For example, 1, 2, 3 minus 55 gives us 68 1 2 3. Star two gives us 2, 4, 6, 1, 2, 3 divided by two gives us 61.55 divided by two gives us a remainder of one 16
03:35
to the power of 0.5. That is the square root of 16 gives us the value. Four, just like other scenes inspired programming languages, JavaScript also has the double plus and the double minus operators along with a prefix and postfix support. So you can take a variable and add plus plus after the variable or plus plus before the variable. And both of these will increment the variable. For example, we start with zero, we go to one and then we go to two. And the same is true for minus minus as well. We can do example minus minus to get minus one or minus minus example to get minus two.
04:08
The difference between the postfix version and the prefix version becomes obvious when we try to do the same within an assignment expression. Here we have the same code written twice, but the first time we are using the postfix plus plus. And in the second example we are using the prefix plus plus to understand the difference, focus on the terms post and pre. With post plus plus the runtime returns the current value immediately and post that we update the value of the variable. So the value that goes into the result is the original value, which is zero, and then example gets updated to the value of one. Similarly,
04:44
with pre plus plus the value gets pre-up updated. So example is immediately going to become one and that is what is returned into the result. And the same thing applies to the minus, minus postfix and prefix operators. With postfix, the result is going to be the value before the update and with prefix the result is going to be the value with the minus minus pre-applied. Now share income is a surprise that these operators only work on variables that can be muted. For example, alpha is a lead variable and therefore alpha plus plus is perfectly fine. However, if you just have three standing by itself,
05:16
it's not actually a variable. So three plus plus is going to result in an error. And similarly, if we have cons beta, then beta is a cons and therefore it cannot be reassigned and beta plus plus will not be allowed. And of course if you have touched it configured as we did in a previous lesson, then it'll catch these silly mistakes for you at compiled time. To make core execution decisions based on a value, we need to be able to compare numbers and of course JavaScript comparison operators have got your back. You can compare to numbers with a less than operator and of course one is less than three. So that's true.
05:49
You can use the greater than operator one is a not greater than three and therefore that is false. There is also less than or equal to, for example, three is less than or equal to three because it is equal to three and that will be true. There is greater than or equal and four is not greater than or equal to five, which is actually less than five. So therefore it'll be false. And there is also strict equality. For example, one is equal to one and of course that's going to be true and strict inequality, which is not equal equal. So five is not equal to three and therefore that is true because DAOs cap
06:24
numbers have a fixed bit size of 64. There are limitations on the size of inte that it can safely represent. There is a global number module that has dismember called max safe integer, which represents the max safe positive value. And similarly there is a min safe integer which represents the minimum safe value. Now safe in this context refers to the fact that the value cannot be result of a rounding error. The unsafe values are plus one away from max safe and minus one away from mins safe.
06:54
And what that basically means is that there is no guarantee that the result that we get at this point is not the result of a rounding error. So max safe plus one while correct is ambiguous because plus two will give us the same value. So max safe and mins safe, you are perfectly fine Outside of that range, numbers can be represented but they might be a chance that they might have been rounded. Now to check if there is a chance that a rounding might have occurred on the value, you can use this function called number is safe integer and of course max safe integer is safe,
07:27
but anything else outside of that range is not going to be safe. For example, plus one is not going to be safe. And similarly, any other number that we write that is going to be outside that range is not going to be safe. If you go out of pounds of what values can be represented in the 64 bits, you will end with infinity, which is still a well-defined numeric value within JavaScript. As an example, if you decide that you have had enough and raise 10 to the power 1000, you will get infinity. And if you multiply it with minus one, you will get minus infinity. Dividing a positive number by zero will give you infinity and dividing a
08:02
negative number by zero will give you negative infinity. Now as I mentioned, these are well-defined literals, so you can actually type it in yourself as well and it is equal to itself. So infinity is equal to infinity minus infinity is equal to minus infinity and they've work with comparison operators as well. So infinity is greater than any other positive number that you can think of and a minus infinity is smaller than any other negative number that you can think of. Now these values also exist as members of the number module. So number has positive infinity, which is of course equal to infinity and negative infinity,
08:35
which is of course equal to minus infinity. Since the origin of JavaScript lies in user interface programming working with strings is often required and so the interop between numbers and strings is quite well built within JavaScript. Let's first look at converting a number into a string. Now numbers have a method called two string, which Can be used to convert the number into a string, and this actually gets in worked automatically when we add a number to a string and we've seen examples of that in our string lesson. When we are working with floating point values, we might want to format it a bit further because the default to string might
09:09
give us a really long string that we might not like For a bit of formatting control, there is a function built into strings called two fixed, which takes a number of decimal points that we want to display. So if we invoke two fixed on change and pass in a value of two, we get back a string that contains two decimal digits. Additionally, two fixed will also round a value. So as an example, if we invoke two fixed one, it rounds it up to four. Now Tofi will always try to add the required number of digits, so if we don't have any, for example, 42 clearly doesn't. It'll add zeros instead.
09:43
Now let's talk about the opposite where we take a string and we try to pass it into a number. Chow script has a global function available for us called pass int, which takes a string and tries to pass it into its integer equivalent. If the string in question has a decimal portion, that portion is completely ignored by par int. So 23.55, we only get back the number 23. Now if we do want to pass the value as a float, then JavaScript has another built-in function called pass float, which takes that string and passes it into a float. So 23.55 string gives us the numeric 23.55. Now,
10:18
just like we use the double bank to convert other values into Drew Bullions, JavaScript has a ary plus operator that can be used to convert a string into their number equivalent. When I have a string that I trust is going to pass correctly, then this urinary plus is normally what I would use. But what about the case when we have a string that is not going to pass correctly, for example, sugar spice and everything nice, although excellent is not a valid number for these cases, JavaScript has a special numeric value called nan, which stands for not a number beyond passing things that clearly cannot be a
10:55
number. The Nan literal also shows up in other scenarios. For example, when we try to divide zero by zero, there is a very peculiar but completely reasonable behavior observative within NANS that is best explained with a simple example. Here we have two variables, first and second, which are the result of passing two different strings. Now, both of these are going to be Nan, but clearly I'll find we are not equal to each other. Therefore it makes reasonable sense that first should not be equal to second. And indeed this is the case within JavaScript. However, this does mean that a nan is not equal to a nan.
11:33
This poses a slight problem because if a nan is not equal to nan, then a nan equal to NAN will be false. This will actually prevent us from checking if a particular value has resulted in an N, but fortunately, JavaScript has a very nice global function called is N, that will tell you if a particular numeric value is an N Or not. And as a side note, these silly mistakes of comparing directly with an NAN instead of using the Isan function are caught as a compile time error per type script in addition to literals in the decimal number system.
12:05
The number TE type also allows us to express our ideas in other digital basis, but make no mistake, they are still backed by the same boundary. 64 bit value. Consider the simple inte J 32, which is of course two to the power five. Representing this value in binary should be one followed by five zeros, and we can actually write that literal within towers script with zero B followed by the boundary value. Note that this is still the same number underneath, which is 32, which is of course Y perfect is equivalent to this boundary literal. In addition to boundary,
12:39
we can do octal with zero O and this is four zero and octal and hexa decimal with zero x and this is two zero in hexa decimal. Now all of these temptations are case insensitive as well, so it doesn't matter if b O or X are lowercase or uppercase. In addition to expressing ourselves in these numeric lits, we can actually take a numeric value and express it out as a string in any of these number systems. Now, by default, two string will render it out in decimal, but two string actually optionally also takes in a base for the number system. For example,
13:12
binary is base two and we get that one followed by five zeros and similarly octal is eight. We get 40 and hex is 16 and we get 20. And now that we know how to express these numbers as strings in different bases, we can actually pass them from the different bases as well. Now by default pass int, we'll pass it as a decimal. However, we can pass in a second optional argument to pass int for the base, for example, two for binary, eight for octal, and 16 for hex a decimal. All of these strings in the different bases, we'll pass to the numeric value of 32.
13:46
JavaScript comes with a built-in global math module that provides a lot of utility functions and some constraints that make it easier to work with numbers. For example, there is a math dot floor which will get rid of the floating portion. There is math dot seal, which if there is a floating portion, it'll bump up the integer value. So here 1.01 becomes two, and then there is math start round, which will round it to the nearest integer most smartly. For example, 1.99 becomes two, 1.01 becomes one, and 1.5 gets rounded up to two. And there is also math, maths, random,
14:21
which is perhaps my favorite function, and it returns a floating point starting from zero, but ending right before one. And by combining this with multiplication and math flow, we can actually scale it to whatever value that we want. For example, if you multiply it by two, it is going to be in the range zero to 1.9 9, 9, 9 9. And therefore if you flow it, it is either going to be zero or it is going to be one. And similarly, if you want six distinct intes, multiply it by six and use math flow To get zero to five. Math also has functions Maths max and math do min max returns the
14:57
maximum value within a set of numbers and min returns the smallest value within a set of numbers. And these functions come in quite handy. And speaking of handy math also has some Constance, for example, math. Do pi, which comes in handy when you are working with circles. For example. Here's the circle front of a unit circle. As you can see, there is a lot of power built into JavaScript numbers and for most user interface programming purposes, you will be well looked after. As always, thank you for joining me and I will see you in the next one.