1
00:00:00,240 --> 00:00:05,010
We have briefly in the earlier lessons, looked at some data types, and we're going to look at this

2
00:00:05,010 --> 00:00:11,280
in more detail in this lesson, that script standard, there's several different standard data types.

3
00:00:11,550 --> 00:00:12,740
So there are booleans.

4
00:00:12,740 --> 00:00:14,850
So we know those are either true or false.

5
00:00:14,880 --> 00:00:15,860
There's numbers.

6
00:00:15,870 --> 00:00:18,840
So those can be written with or without a decimal place.

7
00:00:18,960 --> 00:00:22,690
There's strings so they can be written with a single or double quotes.

8
00:00:22,740 --> 00:00:24,090
There's also NUL.

9
00:00:24,090 --> 00:00:25,890
So this is explicitly nulls.

10
00:00:25,890 --> 00:00:26,760
So there's nothing.

11
00:00:26,760 --> 00:00:31,980
And by default, when you declare a variable and if you haven't assign any value to it, then it's comes

12
00:00:31,980 --> 00:00:35,820
back as undefined and there's also symbol and symbol value.

13
00:00:35,830 --> 00:00:38,670
So it represents a unique identifier symbol.

14
00:00:38,670 --> 00:00:46,740
It's new in ECMAScript 2015 and it's data type whose instances are unique and immutable and everything

15
00:00:46,740 --> 00:00:50,850
else or almost everything else in JavaScript is an object type.

16
00:00:50,910 --> 00:00:54,300
You can also, with JavaScript, convert between different types.

17
00:00:54,300 --> 00:00:59,400
And we have seen this as well, where when we've got we can start out with a boolean or we can start

18
00:00:59,400 --> 00:01:04,110
it with the string and then when we assign a number to it, the data type becomes a number.

19
00:01:04,260 --> 00:01:10,090
So there is flexibility with JavaScript, but you can also force the different data types and the type

20
00:01:10,110 --> 00:01:16,920
conversion so you can convert a number to a string by wrapping it with the string methods typing in

21
00:01:16,920 --> 00:01:21,480
string and then the rounded brackets and then using the value or the variable.

22
00:01:21,480 --> 00:01:22,500
That's a number.

23
00:01:22,590 --> 00:01:24,320
And that can turn that into a string.

24
00:01:24,330 --> 00:01:29,190
And there's also arrays that can be turned into strings and there's more on arrays coming in the upcoming

25
00:01:29,190 --> 00:01:29,660
lessons.

26
00:01:29,670 --> 00:01:36,540
You can also use the dot to string methods are changing the one value together with another one, and

27
00:01:36,540 --> 00:01:41,400
that's going to turn it into a string so you can convert it into a number so you can force a string.

28
00:01:41,400 --> 00:01:44,400
And as you can see, the 10 has the quotes around it.

29
00:01:44,410 --> 00:01:45,330
So this is a number.

30
00:01:45,570 --> 00:01:48,540
You can also turn a boolean into a number.

31
00:01:48,780 --> 00:01:54,000
And if you try to turn something that is not a number, could never be a number into a number, you're

32
00:01:54,000 --> 00:01:54,630
going to get this.

33
00:01:54,870 --> 00:01:57,990
And again, so that stands for not a number.

34
00:01:58,170 --> 00:02:03,050
And we can all log these out into the console and try these and take a closer look at this.

35
00:02:03,150 --> 00:02:07,950
There's also type core coersion, which is converted by JavaScript.

36
00:02:07,950 --> 00:02:12,720
If we add a number to a number or if we had a string or something, that's not a number to a number,

37
00:02:12,870 --> 00:02:17,940
then if it can't be converted into a number, then it's going to automatically turn it into a string.

38
00:02:18,090 --> 00:02:24,180
So JavaScript tries to do some of the thinking for us and trying to convert the different values into

39
00:02:24,180 --> 00:02:25,320
the appropriate data type.

40
00:02:25,470 --> 00:02:31,080
And more often than not, this is a common issue when you have values that you think are numbers and

41
00:02:31,080 --> 00:02:36,660
you try to add them together and it turns out to do ten plus five and it ends up with something like

42
00:02:36,840 --> 00:02:39,530
one or five instead of 15.

43
00:02:39,810 --> 00:02:46,650
So we find that when we are doing data types, sometimes it's a good idea to force the value and make

44
00:02:46,650 --> 00:02:49,200
sure that it is the data type that we are trying to work with.

45
00:02:49,230 --> 00:02:51,960
You can also have some fun with the different data types.

46
00:02:51,990 --> 00:02:56,030
So one of the JavaScript quirks is that the data types are dynamic.

47
00:02:56,040 --> 00:03:01,470
So this means that JavaScript does try to convert it and we can of course, convert it with the number

48
00:03:01,470 --> 00:03:05,070
or string methods which allow us to force various data types.

49
00:03:05,230 --> 00:03:07,040
And as you can see in these examples.

50
00:03:07,170 --> 00:03:10,890
So if we do hello plus world, we end up with a string hello world.

51
00:03:11,070 --> 00:03:17,670
If we do five, which is a string plus a number string, we're going to end up with fifty five, ten

52
00:03:17,850 --> 00:03:19,400
minus five.

53
00:03:19,440 --> 00:03:24,990
We're not going to be able to subtract it, but it is going to convert this into a number and then we're

54
00:03:24,990 --> 00:03:25,860
going to end up with five.

55
00:03:25,870 --> 00:03:30,540
So there are a bunch of little strange things that do happen with JavaScript and you can try these out

56
00:03:30,540 --> 00:03:36,540
in the console in order to get more familiar with it syntax to use in order to get data types.

57
00:03:36,840 --> 00:03:39,960
So if we do, type of that will return the different data types.

58
00:03:39,960 --> 00:03:45,060
I'll show you how to do that in the console and then also output that information into the console.

59
00:03:45,300 --> 00:03:51,120
So if we have a five and we want to find out what data type it is, we want to output that value and

60
00:03:51,120 --> 00:03:53,040
see what data type the five is.

61
00:03:53,190 --> 00:03:58,410
So opening up our browser and we can see that we have a value that's a string.

62
00:03:58,620 --> 00:04:03,960
So if we have a string and if we add a five to it, we end up with the 55 string.

63
00:04:04,170 --> 00:04:08,400
If we have a 10 and we subtract a five to it, we end up with a five.

64
00:04:08,620 --> 00:04:14,790
So that's one of the weird quirks of JavaScript, is that this one, although they both started out

65
00:04:14,790 --> 00:04:20,820
as strings, when you subtract it, then it automatically thinks, OK, well, because we can't subtract

66
00:04:20,850 --> 00:04:22,170
one string from another.

67
00:04:22,260 --> 00:04:26,730
So then it automatically tries to convert this first one to a number and this is what happens.

68
00:04:26,940 --> 00:04:31,860
So this is what I meant by there are some weird quirks that will happen with JavaScript.

69
00:04:32,040 --> 00:04:37,740
So if we take something like hello and if we subtract world from it, we're going to see that.

70
00:04:37,920 --> 00:04:44,070
And and so that's not a number because of course, we can't subtract words one from another.

71
00:04:44,080 --> 00:04:45,570
So that's a little bit confusing.

72
00:04:45,750 --> 00:04:49,260
And JavaScript tries to address it and adjust it.

73
00:04:49,260 --> 00:04:52,140
And if it can't, then it simply returns back that end.

74
00:04:52,190 --> 00:04:54,620
And you can also set up a variable.

75
00:04:54,630 --> 00:04:59,700
So if we set up a variable and if we set up a variable of a as five.

76
00:04:59,930 --> 00:05:07,340
Type of a you're going to see that it gets returned back a string type of and if we have a value that

77
00:05:07,340 --> 00:05:10,990
is a number, you can see that it gets returned back as a number.

78
00:05:11,000 --> 00:05:16,370
And also there's type of and this one is going to be a boolean and it's going to know it's a boolean

79
00:05:16,370 --> 00:05:19,000
because booleans, again, are either true or false.

80
00:05:19,010 --> 00:05:20,420
So we know that those are booleans.

81
00:05:20,570 --> 00:05:28,130
If you do type of null, it gets return back as an object type of undefined and that just gets returned

82
00:05:28,130 --> 00:05:32,360
back as undefined because undefined is its own data type.

83
00:05:32,360 --> 00:05:34,610
It is good to become familiar with them.

84
00:05:34,880 --> 00:05:36,730
And as we saw with a.

85
00:05:36,890 --> 00:05:43,520
So we've got a is currently string value and you can convert it to a number, convert it into a number

86
00:05:43,820 --> 00:05:48,620
so we can take the value of string and we can turn it into a number.

87
00:05:48,630 --> 00:05:52,950
So when it's blue it's being returned back as a number, a value of one hundred.

88
00:05:52,970 --> 00:05:59,870
So this is clearly a number and we can convert it into a string using the two string method string as

89
00:05:59,870 --> 00:06:00,070
well.

90
00:06:00,080 --> 00:06:03,440
So there are actually two ways to convert it into a string.

91
00:06:03,440 --> 00:06:06,040
So those are the two examples of how we can convert it.

92
00:06:06,050 --> 00:06:09,230
And then if you ever want to find out the type, all you have to do is type in.

93
00:06:09,230 --> 00:06:10,080
Type of.

94
00:06:10,100 --> 00:06:11,750
So let's go back to our challenge.

95
00:06:11,750 --> 00:06:18,050
And the challenge is to create a string convert to different data types and then get the data type of

96
00:06:18,050 --> 00:06:21,680
the variable and output that type to the console solution.

97
00:06:21,680 --> 00:06:26,960
We're going to create a number, give it a string value of five, then take that string and convert

98
00:06:26,960 --> 00:06:32,020
it into a number, outputted as a number, and then check the data type of the myname.

99
00:06:32,030 --> 00:06:33,750
So make sure that it has converted.

100
00:06:34,040 --> 00:06:35,840
So go ahead and try this out in your editor.

101
00:06:35,840 --> 00:06:37,650
You can pause the video and I'll show you the solution.

102
00:06:37,700 --> 00:06:43,330
Open up the editor and let's set up myname and this can be any number that we want.

103
00:06:43,460 --> 00:06:50,300
So A set my number to one hundred and then we'll take and what we'll do first will use console log and

104
00:06:50,300 --> 00:06:56,450
will output the type of my num so that you can see that it actually is being converted.

105
00:06:56,450 --> 00:06:59,970
So when we refresh we see the type as a string and convert it.

106
00:06:59,990 --> 00:07:04,610
So there is the number and we're taking the variable and we're converting it through.

107
00:07:04,790 --> 00:07:06,680
It's being returned back as a number.

108
00:07:06,950 --> 00:07:14,000
And if we log out the value of my NUM, you can see as well Chrom is outputting it as a number since

109
00:07:14,000 --> 00:07:19,370
it's blue and wait, hold everything before we conclude, I have another challenge for you.

110
00:07:19,400 --> 00:07:20,870
So this is a bonus challenge.

111
00:07:20,960 --> 00:07:26,660
Did you get a chance to try some of that in your console where you're adding maybe two strings together,

112
00:07:26,810 --> 00:07:31,550
maybe two numbers together and maybe a string and a number and see what happens?

113
00:07:31,730 --> 00:07:41,270
So try that out as well and output the value of five to ten and then fix A plus B to equal 15.

114
00:07:41,270 --> 00:07:45,080
And remember converting those two numbers in order to add them together.

115
00:07:45,170 --> 00:07:54,770
So the solution for this one setting up a as five, setting up B as ten, and then we want to do is

116
00:07:54,770 --> 00:08:00,050
add those together in the console, but we want to have a value of 50 instead.

117
00:08:00,080 --> 00:08:01,730
Right now we're getting five, ten.

118
00:08:01,730 --> 00:08:02,660
So let's fix that.

119
00:08:02,900 --> 00:08:09,860
And we can convert these two numbers by wrapping that with the number method and again wrapping it with

120
00:08:09,860 --> 00:08:12,980
the rounded brackets and having the value passed inside.

121
00:08:13,170 --> 00:08:20,210
So that will force A and B to be numeric and in fact, allowing us to add them together and return back

122
00:08:20,210 --> 00:08:20,680
15.

123
00:08:21,440 --> 00:08:22,930
So try that one out as well.
