All language subtitles for 3. Converting and Checking Numbers

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:01,470 --> 00:00:03,190 Let's start this section 2 2 00:00:03,190 --> 00:00:06,520 by learning how numbers work in JavaScript, 3 3 00:00:06,520 --> 00:00:09,340 how to convert values to numbers 4 4 00:00:09,340 --> 00:00:13,713 and also how to check if certain values are numbers or not. 5 5 00:00:15,570 --> 00:00:17,424 And before starting this section, 6 6 00:00:17,424 --> 00:00:20,400 go ahead and open up your starter files 7 7 00:00:20,400 --> 00:00:23,680 for this section so that we can take a look here 8 8 00:00:23,680 --> 00:00:26,220 at the script.js. 9 9 00:00:26,220 --> 00:00:28,759 So as I said, in the last section, 10 10 00:00:28,759 --> 00:00:31,490 in this one, we will continue working 11 11 00:00:31,490 --> 00:00:34,532 on our Bankist application, okay? 12 12 00:00:34,532 --> 00:00:37,810 Now, what's different here 13 13 00:00:37,810 --> 00:00:40,930 is that now we have actually different data. 14 14 00:00:40,930 --> 00:00:44,793 So we have only two accounts now here 15 15 00:00:44,793 --> 00:00:48,210 and each account now has a new array, 16 16 00:00:48,210 --> 00:00:50,853 which contains the dates of the movements. 17 17 00:00:51,830 --> 00:00:53,910 We also have the currency property 18 18 00:00:53,910 --> 00:00:56,720 and a locale property. 19 19 00:00:56,720 --> 00:00:59,670 Now, right, and so we will use this data 20 20 00:00:59,670 --> 00:01:03,650 in later lectures of this section, okay? 21 21 00:01:03,650 --> 00:01:06,550 Then down here, we have the complete application 22 22 00:01:06,550 --> 00:01:08,850 that we built in the last section. 23 23 00:01:08,850 --> 00:01:10,820 And if you want, you can go ahead 24 24 00:01:10,820 --> 00:01:14,510 and replace this entire code that I have here 25 25 00:01:14,510 --> 00:01:18,300 with your own code from the last section, okay? 26 26 00:01:18,300 --> 00:01:21,560 What matters here is that we make the application work 27 27 00:01:21,560 --> 00:01:25,683 just as it was working by the end of the last section. 28 28 00:01:26,650 --> 00:01:27,780 All right? 29 29 00:01:27,780 --> 00:01:30,970 So again, we have the same application, 30 30 00:01:30,970 --> 00:01:33,700 only the starter data is a little bit different 31 31 00:01:33,700 --> 00:01:36,310 but everything else is the same. 32 32 00:01:36,310 --> 00:01:41,090 And so now let's start learning a little bit about numbers. 33 33 00:01:41,090 --> 00:01:43,680 And the first thing that you should know about numbers 34 34 00:01:43,680 --> 00:01:45,190 is that in JavaScript, 35 35 00:01:45,190 --> 00:01:47,940 all numbers are presented internally 36 36 00:01:47,940 --> 00:01:50,140 as floating point numbers. 37 37 00:01:50,140 --> 00:01:52,720 So basically, always as decimals, 38 38 00:01:52,720 --> 00:01:55,160 no matter if we actually write them 39 39 00:01:55,160 --> 00:01:59,020 as integers or as decimals. 40 40 00:01:59,020 --> 00:02:01,043 So we can show that by doing this. 41 41 00:02:03,770 --> 00:02:05,543 So we need a new terminal here. 42 42 00:02:10,640 --> 00:02:13,053 And so here is our application back again. 43 43 00:02:13,980 --> 00:02:16,763 Let's close this and open up the console. 44 44 00:02:17,940 --> 00:02:19,140 Give it some more space 45 45 00:02:20,300 --> 00:02:25,300 and so you see that 23 is, in fact, the same as 23.0. 46 46 00:02:26,010 --> 00:02:27,050 Okay? 47 47 00:02:27,050 --> 00:02:29,800 And that's the reason why we only have one data type 48 48 00:02:29,800 --> 00:02:31,560 for all numbers. 49 49 00:02:31,560 --> 00:02:34,360 Also, numbers are represented internally 50 50 00:02:34,360 --> 00:02:37,440 in a 64 base 2 format. 51 51 00:02:37,440 --> 00:02:40,230 So that means that numbers are always stored 52 52 00:02:40,230 --> 00:02:42,020 in a binary format. 53 53 00:02:42,020 --> 00:02:46,720 So basically, they're only composed of zeros and ones. 54 54 00:02:46,720 --> 00:02:48,630 Now, in this binary form, 55 55 00:02:48,630 --> 00:02:51,720 it is very hard to represent some fractions 56 56 00:02:51,720 --> 00:02:54,000 that are very easy to represent 57 57 00:02:54,000 --> 00:02:57,170 in the base 10 system that we are used to. 58 58 00:02:57,170 --> 00:03:02,120 So base 10 is basically the numbers from zero to nine, 59 59 00:03:02,120 --> 00:03:05,850 while binary is base 2 60 60 00:03:05,850 --> 00:03:10,460 and so that's the numbers zero and one. 61 61 00:03:10,460 --> 00:03:11,610 Okay? 62 62 00:03:11,610 --> 00:03:14,190 So as I was saying, there are certain numbers 63 63 00:03:14,190 --> 00:03:17,540 that are very difficult to represent in base 2. 64 64 00:03:17,540 --> 00:03:21,822 And one example of that is the fraction 0.1. 65 65 00:03:21,822 --> 00:03:26,446 And that then results in very weird results like this. 66 66 00:03:26,446 --> 00:03:29,550 And this is kind of a classic 67 67 00:03:29,550 --> 00:03:32,210 or a running joke in JavaScript 68 68 00:03:32,210 --> 00:03:35,177 because this result should, of course, be 0.3 69 69 00:03:36,150 --> 00:03:38,740 and not with all of these zeros here. 70 70 00:03:38,740 --> 00:03:41,486 But JavaScript simply has no better way 71 71 00:03:41,486 --> 00:03:43,570 of representing this number. 72 72 00:03:43,570 --> 00:03:45,970 So in base 10, 73 73 00:03:45,970 --> 00:03:50,970 1/10 is simply 0.1. 74 74 00:03:52,510 --> 00:03:54,820 And so that's very easy to represent. 75 75 00:03:54,820 --> 00:03:58,013 But, for example, if we were trying to do 3/10, 76 76 00:03:59,260 --> 00:04:03,700 then that is also impossible to represent for us, right? 77 77 00:04:03,700 --> 00:04:05,700 It would be this number here 78 78 00:04:05,700 --> 00:04:08,890 and three until infinity, okay? 79 79 00:04:08,890 --> 00:04:13,600 And so in binary, the same thing happens with 0.1. 80 80 00:04:13,600 --> 00:04:15,990 So we get basically an infinite fraction 81 81 00:04:15,990 --> 00:04:19,793 and that then results in a weird result like this one. 82 82 00:04:20,670 --> 00:04:23,540 Now, in some cases, JavaScript does some rounding 83 83 00:04:23,540 --> 00:04:25,760 behind the scenes to try its best 84 84 00:04:25,760 --> 00:04:27,740 to hide these problems 85 85 00:04:27,740 --> 00:04:30,310 but some operations, such as this one, 86 86 00:04:30,310 --> 00:04:32,620 simply cannot mask the fact 87 87 00:04:32,620 --> 00:04:33,760 that behind the scenes, 88 88 00:04:33,760 --> 00:04:36,750 they cannot represent certain fractions. 89 89 00:04:36,750 --> 00:04:40,130 And by the way, many other languages use the same system. 90 90 00:04:40,130 --> 00:04:42,580 For example, PHP or Ruby 91 91 00:04:42,580 --> 00:04:45,610 and so don't let anyone make fun of JavaScript 92 92 00:04:45,610 --> 00:04:47,840 because of this, okay? 93 93 00:04:47,840 --> 00:04:50,540 So we just have to accept that it works this way 94 94 00:04:50,540 --> 00:04:53,800 because we really cannot do anything against this. 95 95 00:04:53,800 --> 00:04:54,980 So just be aware 96 96 00:04:54,980 --> 00:04:58,740 that you cannot do like really precise scientific 97 97 00:04:58,740 --> 00:05:01,480 or financial calculations in JavaScript 98 98 00:05:01,480 --> 00:05:05,692 because eventually, you will run into a problem like this. 99 99 00:05:05,692 --> 00:05:10,692 So .2, which will result in false, 100 100 00:05:11,290 --> 00:05:14,140 which, of course, is incorrect as we know. 101 101 00:05:14,140 --> 00:05:17,550 This should be true but well, this is simply an error 102 102 00:05:17,550 --> 00:05:22,117 in JavaScript that we have to accept, okay? 103 103 00:05:22,117 --> 00:05:24,320 All right, now that we know 104 104 00:05:24,320 --> 00:05:26,690 how JavaScript represents numbers, 105 105 00:05:26,690 --> 00:05:29,690 let's go back to actually working with them. 106 106 00:05:29,690 --> 00:05:34,167 So we know how to convert a string to a number, right? 107 107 00:05:34,167 --> 00:05:37,610 So that's like this. 108 108 00:05:37,610 --> 00:05:40,050 But there's also an easier way. 109 109 00:05:40,050 --> 00:05:42,420 So kind of a trick that we can use, 110 110 00:05:42,420 --> 00:05:46,950 which is to, or actually this should be a string, 111 111 00:05:46,950 --> 00:05:49,173 otherwise that doesn't make much sense. 112 112 00:05:50,090 --> 00:05:51,480 But as I was saying, 113 113 00:05:51,480 --> 00:05:52,860 there is an easier way, 114 114 00:05:52,860 --> 00:05:56,600 which is simply plus 23 the string. 115 115 00:05:56,600 --> 00:05:57,433 And this works 116 116 00:05:57,433 --> 00:06:01,020 because when JavaScript sees the plus operator, 117 117 00:06:01,020 --> 00:06:02,930 it will do type coercion. 118 118 00:06:02,930 --> 00:06:05,010 So it will automatically convert all 119 119 00:06:05,010 --> 00:06:06,910 the operands to numbers. 120 120 00:06:06,910 --> 00:06:11,550 And so therefore, we see 23 here in both these cases. 121 121 00:06:11,550 --> 00:06:14,393 So in this purple color, which are numbers. 122 122 00:06:16,470 --> 00:06:19,530 So this here makes our code look a lot cleaner 123 123 00:06:19,530 --> 00:06:20,720 in my opinion 124 124 00:06:20,720 --> 00:06:23,450 and so let's now actually go to our project 125 125 00:06:23,450 --> 00:06:26,130 and replace all the occurrences of Number, 126 126 00:06:26,130 --> 00:06:28,503 simply with the plus sign. 127 127 00:06:29,700 --> 00:06:31,043 So here. 128 128 00:06:32,780 --> 00:06:35,530 And if I save it now, the Prettier extension 129 129 00:06:35,530 --> 00:06:38,020 should get rid of the parentheses here. 130 130 00:06:38,020 --> 00:06:38,923 And it does. 131 131 00:06:41,960 --> 00:06:44,003 So here and here. 132 132 00:06:46,980 --> 00:06:48,463 And here as well. 133 133 00:06:51,940 --> 00:06:53,173 And that should be it. 134 134 00:06:54,720 --> 00:06:55,853 Yeah, that's it. 135 135 00:06:56,750 --> 00:06:57,810 Great. 136 136 00:06:57,810 --> 00:07:00,070 And so our project will work the same 137 137 00:07:00,070 --> 00:07:02,120 but the code is a little bit cleaner now. 138 138 00:07:02,990 --> 00:07:07,083 Okay, so here we converted strings to numbers. 139 139 00:07:08,700 --> 00:07:11,793 But we can also do something called parsing. 140 140 00:07:13,210 --> 00:07:15,883 So we can parse a number from a string. 141 141 00:07:17,980 --> 00:07:19,950 So on the Number object, 142 142 00:07:19,950 --> 00:07:22,140 which is kind of this function here, 143 143 00:07:22,140 --> 00:07:23,910 but it's also an object in the end. 144 144 00:07:23,910 --> 00:07:28,040 Remember because every function is also an object. 145 145 00:07:28,040 --> 00:07:29,540 And this number object here 146 146 00:07:29,540 --> 00:07:32,363 has some methods to do parsing. 147 147 00:07:33,200 --> 00:07:34,890 So let's use parseInt 148 148 00:07:37,050 --> 00:07:39,950 and so here we can now specify a string 149 149 00:07:40,800 --> 00:07:44,330 and that string can even include some symbols. 150 150 00:07:44,330 --> 00:07:46,500 And JavaScript will then automatically try 151 151 00:07:46,500 --> 00:07:48,180 to figure out the number 152 152 00:07:48,180 --> 00:07:49,860 that is in this string. 153 153 00:07:49,860 --> 00:07:50,693 All right? 154 154 00:07:50,693 --> 00:07:52,750 And so here we get 30. 155 155 00:07:52,750 --> 00:07:54,410 And this is actually a number. 156 156 00:07:54,410 --> 00:07:55,950 It's not a string. 157 157 00:07:55,950 --> 00:07:57,280 All right? 158 158 00:07:57,280 --> 00:07:58,905 Now, in order to make this work, 159 159 00:07:58,905 --> 00:08:03,140 the string needs to start with a number. 160 160 00:08:03,140 --> 00:08:05,890 So if we have something like this, 161 161 00:08:05,890 --> 00:08:06,917 then it's not gonna work 162 162 00:08:06,917 --> 00:08:09,250 and we get not a number. 163 163 00:08:09,250 --> 00:08:10,260 Okay? 164 164 00:08:10,260 --> 00:08:13,180 So this is a little bit like type coercion 165 165 00:08:13,180 --> 00:08:14,810 but even more advanced 166 166 00:08:14,810 --> 00:08:17,320 because as we just saw, 167 167 00:08:17,320 --> 00:08:20,740 it tries to get rid of unnecessary symbols 168 168 00:08:20,740 --> 00:08:22,520 that are not numbers. 169 169 00:08:22,520 --> 00:08:24,930 And this can be very useful, for example, 170 170 00:08:24,930 --> 00:08:29,230 in this situation where we get some kind of unit from CSS 171 171 00:08:29,230 --> 00:08:32,710 and then need to get rid of that unit. 172 172 00:08:32,710 --> 00:08:33,909 Now, the parseInt function 173 173 00:08:33,909 --> 00:08:37,090 actually accepts a second argument, 174 174 00:08:37,090 --> 00:08:39,570 which is the so-called regex. 175 175 00:08:39,570 --> 00:08:42,530 And the regex is the base of the numeral system 176 176 00:08:42,530 --> 00:08:44,150 that we are using. 177 177 00:08:44,150 --> 00:08:47,460 So here we are simply using base 10 numbers. 178 178 00:08:47,460 --> 00:08:50,140 So numbers from zero to nine. 179 179 00:08:50,140 --> 00:08:52,890 And most of the time, we are doing that 180 180 00:08:52,890 --> 00:08:55,773 and so we should always pass in the number 10 here. 181 181 00:08:57,660 --> 00:09:01,970 Okay, so that can avoid some bugs in some situations. 182 182 00:09:01,970 --> 00:09:04,500 And you see the result here is the same. 183 183 00:09:04,500 --> 00:09:07,450 But if we were working, for example, with binary, 184 184 00:09:07,450 --> 00:09:09,840 then we would write two 185 185 00:09:09,840 --> 00:09:12,963 and then the result would be completely different. 186 186 00:09:14,090 --> 00:09:16,610 In this case, we cannot even see it. 187 187 00:09:16,610 --> 00:09:19,000 But let's just keep using 10 here. 188 188 00:09:19,000 --> 00:09:22,260 So this is with integers. 189 189 00:09:22,260 --> 00:09:24,660 So that's what the int here stands for. 190 190 00:09:24,660 --> 00:09:29,294 So parse integers but there's also parseFloat. 191 191 00:09:29,294 --> 00:09:34,294 So Number.parseFloat 192 192 00:09:35,730 --> 00:09:38,193 and let's now try 2.5rem. 193 193 00:09:41,830 --> 00:09:44,940 And so now it reads the decimal number here 194 194 00:09:44,940 --> 00:09:46,420 from our string. 195 195 00:09:46,420 --> 00:09:48,760 So a floating point number. 196 196 00:09:48,760 --> 00:09:52,053 Let's see what happens if we used Int. 197 197 00:09:53,230 --> 00:09:57,340 And then we only get the integer part, right? 198 198 00:09:57,340 --> 00:10:00,970 So it will then stop here at this decimal point 199 199 00:10:00,970 --> 00:10:03,660 but that, of course, not what we want. 200 200 00:10:03,660 --> 00:10:04,493 And by the way, 201 201 00:10:04,493 --> 00:10:08,160 we could even have like some white space down here 202 202 00:10:08,160 --> 00:10:11,383 that would not affect anything at all. 203 203 00:10:12,694 --> 00:10:14,060 All right? 204 204 00:10:14,060 --> 00:10:17,060 Now, by the way, these two functions here 205 205 00:10:17,060 --> 00:10:20,483 are actually also so-called global functions. 206 206 00:10:22,090 --> 00:10:24,833 So we would not have to call them on Number. 207 207 00:10:26,570 --> 00:10:28,383 So this here also works. 208 208 00:10:29,380 --> 00:10:31,890 Okay, but this is the more traditional 209 209 00:10:31,890 --> 00:10:34,150 and old-school way of doing it. 210 210 00:10:34,150 --> 00:10:35,590 Now in modern JavaScript, 211 211 00:10:35,590 --> 00:10:38,860 it is more encouraged to call these functions 212 212 00:10:38,860 --> 00:10:42,320 actually on the Number object, okay? 213 213 00:10:42,320 --> 00:10:46,010 So we say that Number here provides something 214 214 00:10:46,010 --> 00:10:48,530 called a namespace, all right? 215 215 00:10:48,530 --> 00:10:51,560 So a namespace for all these different functions, 216 216 00:10:51,560 --> 00:10:54,213 like parseFloat, and parseInt. 217 217 00:10:55,470 --> 00:10:58,900 But anyway, let's now explore another function 218 218 00:10:58,900 --> 00:11:01,310 of the Number namespace 219 219 00:11:01,310 --> 00:11:04,070 and that is the isNaN. 220 220 00:11:04,070 --> 00:11:05,573 So is not a number. 221 221 00:11:08,010 --> 00:11:09,583 So Number.IsNotaNumber. 222 222 00:11:11,740 --> 00:11:13,270 And we can use this one 223 223 00:11:13,270 --> 00:11:16,880 to basically check if any value is a number. 224 224 00:11:16,880 --> 00:11:20,750 Well, not any value but more about that later. 225 225 00:11:20,750 --> 00:11:23,570 So for example, if we pass 20, 226 226 00:11:23,570 --> 00:11:26,830 then here we got false. 227 227 00:11:26,830 --> 00:11:28,610 And so is it not a number? 228 228 00:11:28,610 --> 00:11:30,890 False, all right? 229 229 00:11:30,890 --> 00:11:33,133 So let's try the same with a string. 230 230 00:11:34,540 --> 00:11:35,373 20. 231 231 00:11:36,300 --> 00:11:39,510 And so here we also get false, all right? 232 232 00:11:39,510 --> 00:11:42,330 Because this also isn't not a number. 233 233 00:11:42,330 --> 00:11:46,160 It's just a regular value, right? 234 234 00:11:46,160 --> 00:11:49,454 But now let's try it by converting something. 235 235 00:11:49,454 --> 00:11:51,920 So for example, this string, 236 236 00:11:51,920 --> 00:11:54,490 if we try to convert it to a number, 237 237 00:11:54,490 --> 00:11:56,403 then this will be not a number. 238 238 00:11:57,900 --> 00:11:58,733 Right? 239 239 00:11:58,733 --> 00:12:00,293 And so therefore, it is true. 240 240 00:12:01,460 --> 00:12:03,173 So let's just do that here. 241 241 00:12:04,580 --> 00:12:06,283 So yeah, this works as well. 242 242 00:12:07,200 --> 00:12:10,440 And so you see this is, of course, not a number. 243 243 00:12:10,440 --> 00:12:11,750 All right? 244 244 00:12:11,750 --> 00:12:13,653 But let's try something else. 245 245 00:12:15,210 --> 00:12:20,070 Let's, for example, divide 23 by zero 246 246 00:12:20,070 --> 00:12:22,910 and maybe you know that dividing by zero 247 247 00:12:22,910 --> 00:12:25,900 gives us infinite, okay? 248 248 00:12:25,900 --> 00:12:27,810 So dividing by zero is something 249 249 00:12:27,810 --> 00:12:30,180 that's not allowed in mathematics 250 250 00:12:30,180 --> 00:12:33,070 and so it will give us infinity. 251 251 00:12:33,070 --> 00:12:36,600 So infinity is also not not a NaN 252 252 00:12:37,450 --> 00:12:40,223 and so therefore, we get false here as well. 253 253 00:12:41,070 --> 00:12:43,383 So let me just demonstrate that here. 254 254 00:12:44,910 --> 00:12:49,200 So indeed, this gives us this special value of infinity 255 255 00:12:49,200 --> 00:12:52,220 that also exists in JavaScript. 256 256 00:12:52,220 --> 00:12:55,510 And so this is not a number 257 257 00:12:55,510 --> 00:12:57,380 is actually not a perfect way 258 258 00:12:57,380 --> 00:12:59,880 for checking if a value is a number 259 259 00:12:59,880 --> 00:13:03,050 because it doesn't consider this use case 260 260 00:13:03,050 --> 00:13:05,660 and sometimes, this might very well happen. 261 261 00:13:05,660 --> 00:13:09,013 And therefore, there is a better method called isFinite. 262 262 00:13:11,730 --> 00:13:15,800 So Number.isFinite and let's try it 263 263 00:13:15,800 --> 00:13:17,108 with the same numbers. 264 264 00:13:17,108 --> 00:13:19,343 So here we get true. 265 265 00:13:21,060 --> 00:13:24,610 And okay, and so basically this should be the opposite now 266 266 00:13:24,610 --> 00:13:26,090 of this one 267 267 00:13:26,090 --> 00:13:28,800 because this is not not a number. 268 268 00:13:28,800 --> 00:13:30,133 But it is finite. 269 269 00:13:31,300 --> 00:13:32,438 Okay? 270 270 00:13:32,438 --> 00:13:34,593 Now, if you try it with 20, 271 271 00:13:35,740 --> 00:13:39,200 the string, then here we get false 272 272 00:13:39,200 --> 00:13:41,340 because it's not a number. 273 273 00:13:41,340 --> 00:13:43,210 And so this is actually better to check 274 274 00:13:43,210 --> 00:13:45,543 if something is a number or not. 275 275 00:13:47,000 --> 00:13:49,600 And I know this sounds a bit confusing, 276 276 00:13:49,600 --> 00:13:51,511 and the confusion comes from the fact 277 277 00:13:51,511 --> 00:13:53,820 that we have this weird thing here 278 278 00:13:53,820 --> 00:13:56,383 that is also called not a number. 279 279 00:13:57,250 --> 00:13:58,400 All right? 280 280 00:13:58,400 --> 00:14:00,700 And so therefore, I'm just telling you 281 281 00:14:00,700 --> 00:14:04,350 that the isFinite method is indeed the best way 282 282 00:14:04,350 --> 00:14:07,803 of checking if a value is a number. 283 283 00:14:10,640 --> 00:14:13,463 So a real number, not a string. 284 284 00:14:15,320 --> 00:14:16,820 All right? 285 285 00:14:16,820 --> 00:14:18,893 So let's try again this one here. 286 286 00:14:20,440 --> 00:14:21,760 Trying to convert it 287 287 00:14:21,760 --> 00:14:24,670 and we know that this here is not a number. 288 288 00:14:24,670 --> 00:14:27,843 So literally, this will be NaN. 289 289 00:14:28,730 --> 00:14:29,650 Right? 290 290 00:14:29,650 --> 00:14:31,913 And so this should be false as well. 291 291 00:14:33,130 --> 00:14:34,600 And it is. 292 292 00:14:34,600 --> 00:14:36,683 And now finally, this last use case, 293 293 00:14:37,950 --> 00:14:39,623 which gives us infinity. 294 294 00:14:42,060 --> 00:14:44,780 And so it is false as well. 295 295 00:14:44,780 --> 00:14:46,970 So that's where the name of the method 296 296 00:14:46,970 --> 00:14:48,300 actually comes from. 297 297 00:14:48,300 --> 00:14:52,663 So isFinite and so infinity is, of course, not finite. 298 298 00:14:54,110 --> 00:14:55,070 All right? 299 299 00:14:55,070 --> 00:14:57,470 And so this method is the ultimate method 300 300 00:14:57,470 --> 00:15:01,500 that you should use to check if any value is a number, 301 301 00:15:01,500 --> 00:15:04,960 at least when you're working with floating point numbers. 302 302 00:15:04,960 --> 00:15:06,300 So if you are sure 303 303 00:15:06,300 --> 00:15:08,860 that you just need to check for an integer, 304 304 00:15:08,860 --> 00:15:11,113 then you can use isInteger as well. 305 305 00:15:12,598 --> 00:15:16,393 So let me just write here a comment as well. 306 306 00:15:17,540 --> 00:15:19,130 So this one you should only use 307 307 00:15:19,130 --> 00:15:24,130 to check if value is not a number. 308 308 00:15:24,250 --> 00:15:27,934 So literally, not a number this value. 309 309 00:15:27,934 --> 00:15:28,913 Okay? 310 310 00:15:31,070 --> 00:15:32,800 But usually, in practice, 311 311 00:15:32,800 --> 00:15:34,520 I never use this. 312 312 00:15:34,520 --> 00:15:36,660 Whenever I need to check for a number, 313 313 00:15:36,660 --> 00:15:38,440 this is the one to go 314 314 00:15:38,440 --> 00:15:40,820 or if I'm using integers, 315 315 00:15:40,820 --> 00:15:45,273 then I use isInteger as I was just saying before. 316 316 00:15:46,440 --> 00:15:47,893 So we can do this. 317 317 00:15:49,560 --> 00:15:53,470 Also this is also an integer remember 318 318 00:15:53,470 --> 00:15:56,930 and so both are true, okay? 319 319 00:15:56,930 --> 00:16:00,910 But then, of course, if we try this here, 320 320 00:16:00,910 --> 00:16:02,940 it will be false. 321 321 00:16:02,940 --> 00:16:05,550 Now, in our project that we're currently building, 322 322 00:16:05,550 --> 00:16:07,970 there's not really a need to check 323 323 00:16:07,970 --> 00:16:11,260 if any of the inputs is really a number or not 324 324 00:16:11,260 --> 00:16:13,260 and so in this particular case, 325 325 00:16:13,260 --> 00:16:15,730 we don't need to go back and change anything 326 326 00:16:15,730 --> 00:16:17,970 but it's still good to know about, 327 327 00:16:17,970 --> 00:16:20,150 especially this method here. 328 328 00:16:20,150 --> 00:16:21,600 So this is your go-to 329 329 00:16:21,600 --> 00:16:23,010 whenever you need to check 330 330 00:16:23,010 --> 00:16:25,818 if something is a number or not. 331 331 00:16:25,818 --> 00:16:28,270 And going back here, 332 332 00:16:28,270 --> 00:16:30,750 the parseFloat function should be your go-to 333 333 00:16:30,750 --> 00:16:34,284 whenever you need to read a value out of a string. 334 334 00:16:34,284 --> 00:16:37,078 For example, coming from CSS. 335 335 00:16:37,078 --> 00:16:39,010 So then you always get this 336 336 00:16:39,010 --> 00:16:40,330 and it's very nice to be able 337 337 00:16:40,330 --> 00:16:43,023 to read the number out of the string. 27637

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.