WEBVTT

1
00:00:01.130 --> 00:00:02.100
<v Jonas>Now that we're getting</v>

2
00:00:02.100 --> 00:00:04.580
more comfortable with JavaScript,

3
00:00:04.580 --> 00:00:07.880
it's time to learn about a more theoretical concept.

4
00:00:07.880 --> 00:00:08.830
And that's the difference

5
00:00:08.830 --> 00:00:11.980
between statements and expressions.

6
00:00:11.980 --> 00:00:15.510
And this is gonna be just a very high level overview,

7
00:00:15.510 --> 00:00:18.933
just so that the future lecturers make more sense.

8
00:00:20.210 --> 00:00:23.730
So essentially, an expression is a piece of code

9
00:00:23.730 --> 00:00:26.070
that produces a value.

10
00:00:26.070 --> 00:00:29.890
For example, three plus four is an expression

11
00:00:29.890 --> 00:00:33.840
because this is gonna produce a value, okay?

12
00:00:33.840 --> 00:00:36.790
So I don't even mean the entire line of code.

13
00:00:36.790 --> 00:00:39.890
What I mean is really just this three plus four.

14
00:00:39.890 --> 00:00:42.440
This is an expression, okay?

15
00:00:42.440 --> 00:00:46.060
And again, because it does produce a value.

16
00:00:46.060 --> 00:00:50.120
The same goes, for example for just any number.

17
00:00:50.120 --> 00:00:52.310
This for example is an expression too,

18
00:00:52.310 --> 00:00:54.720
even though it's just a simple value.

19
00:00:54.720 --> 00:00:57.300
But it's still also an expression,

20
00:00:57.300 --> 00:00:59.230
because again, this itself

21
00:00:59.230 --> 00:01:01.673
will produce a value in JavaScript.

22
00:01:02.670 --> 00:01:04.110
Or we can do this.

23
00:01:04.110 --> 00:01:08.503
So true and false and,

24
00:01:10.080 --> 00:01:11.770
not false.

25
00:01:11.770 --> 00:01:12.820
So all of this here,

26
00:01:12.820 --> 00:01:14.960
is an expression, right?

27
00:01:14.960 --> 00:01:18.303
Because this will in the end, produce some Boolean value.

28
00:01:19.190 --> 00:01:21.970
Now, on the other hand, we have statements.

29
00:01:21.970 --> 00:01:24.890
And the statement is like a bigger piece of code

30
00:01:24.890 --> 00:01:26.360
that is executed

31
00:01:26.360 --> 00:01:29.840
and which does not produce a value on itself.

32
00:01:29.840 --> 00:01:33.430
And we can compare this with normal spoken language.

33
00:01:33.430 --> 00:01:34.730
And so in this example,

34
00:01:34.730 --> 00:01:37.880
a declaration is like a complete sentence

35
00:01:37.880 --> 00:01:40.340
and expressions are like the words

36
00:01:40.340 --> 00:01:42.680
that make up the sentences.

37
00:01:42.680 --> 00:01:45.430
Now, basically we write our whole programs

38
00:01:45.430 --> 00:01:47.570
as a sequence of actions.

39
00:01:47.570 --> 00:01:50.470
And these actions are statements.

40
00:01:50.470 --> 00:01:53.773
So let's take as an example, the, if else, statement.

41
00:01:54.620 --> 00:01:55.800
So let's say,

42
00:01:55.800 --> 00:01:59.963
if 23 is greater than 10,

43
00:02:01.430 --> 00:02:03.810
then, well just do something.

44
00:02:03.810 --> 00:02:08.787
So create a string called, 23 is bigger.

45
00:02:11.500 --> 00:02:14.230
The code itself doesn't really matter here.

46
00:02:14.230 --> 00:02:18.200
So this, if else statement is in fact a statement.

47
00:02:18.200 --> 00:02:20.350
And so that's the reason why I have called it

48
00:02:20.350 --> 00:02:23.040
the if else statement all the time.

49
00:02:23.040 --> 00:02:26.130
And the same is true for the switch statement.

50
00:02:26.130 --> 00:02:27.510
So this statement here

51
00:02:27.510 --> 00:02:30.430
doesn't really produce a value, does it?

52
00:02:30.430 --> 00:02:32.640
All it does is, in this case,

53
00:02:32.640 --> 00:02:36.810
it simply declares is variable called STR.

54
00:02:36.810 --> 00:02:37.830
But that's it.

55
00:02:37.830 --> 00:02:39.680
It performs some actions,

56
00:02:39.680 --> 00:02:41.650
in this case, declaring this variable,

57
00:02:41.650 --> 00:02:43.540
but it doesn't produce a value.

58
00:02:43.540 --> 00:02:45.130
And so in that, it's different from,

59
00:02:45.130 --> 00:02:48.903
for example, having this here, okay?

60
00:02:49.890 --> 00:02:53.750
Now the string itself, again is an expression.

61
00:02:53.750 --> 00:02:55.780
So this is probably a bit confusing,

62
00:02:55.780 --> 00:02:57.940
but that's not a problem, okay?

63
00:02:57.940 --> 00:03:00.420
My goal here is not that you understand

64
00:03:00.420 --> 00:03:03.300
the exact rules of what an expression is

65
00:03:03.300 --> 00:03:04.750
and what a statement is,

66
00:03:04.750 --> 00:03:08.430
because, in fact, it's all a bit fuzzy.

67
00:03:08.430 --> 00:03:11.130
So my main goal with this lecture is really

68
00:03:11.130 --> 00:03:14.400
that you know that statements and expressions exist,

69
00:03:14.400 --> 00:03:16.630
and that expressions produce values,

70
00:03:16.630 --> 00:03:19.610
and that statements are like full sentences

71
00:03:19.610 --> 00:03:21.770
that translate our actions.

72
00:03:21.770 --> 00:03:25.480
So the actions that we want the program to perform, okay?

73
00:03:25.480 --> 00:03:27.370
So really that's all you need to know,

74
00:03:27.370 --> 00:03:31.080
and all you need to basically memorize from this video.

75
00:03:31.080 --> 00:03:33.210
We don't need to go into all the details.

76
00:03:33.210 --> 00:03:36.760
So there's no need to start asking a bunch of questions

77
00:03:36.760 --> 00:03:40.080
about this in the Q and A section, all right?

78
00:03:40.080 --> 00:03:43.930
So this will all make even more sense as we move on.

79
00:03:43.930 --> 00:03:48.570
So I said that this string here is itself an expression.

80
00:03:48.570 --> 00:03:52.240
And then this whole line of code is a statement,

81
00:03:52.240 --> 00:03:55.000
because this again does not produce a value.

82
00:03:55.000 --> 00:03:58.960
And so basically, whenever something ends with a semicolon,

83
00:03:58.960 --> 00:04:00.970
that's then a statement.

84
00:04:00.970 --> 00:04:03.403
It's like a complete sentence, okay?

85
00:04:04.270 --> 00:04:07.280
Now this difference between expressions and statements

86
00:04:07.280 --> 00:04:08.880
is important to know

87
00:04:08.880 --> 00:04:10.810
because JavaScript expects

88
00:04:10.810 --> 00:04:14.350
statements and expressions in different places.

89
00:04:14.350 --> 00:04:16.680
For example, in a template literal,

90
00:04:16.680 --> 00:04:20.500
we can only insert expressions, but not statements.

91
00:04:20.500 --> 00:04:22.230
So let me just very quickly

92
00:04:23.490 --> 00:04:24.863
demonstrate that to you.

93
00:04:26.180 --> 00:04:28.770
So we can say, I am,

94
00:04:28.770 --> 00:04:31.330
and then our placeholder here, basically.

95
00:04:31.330 --> 00:04:34.260
And so in here, we need to put an expression.

96
00:04:34.260 --> 00:04:36.440
So something that produces a value

97
00:04:36.440 --> 00:04:38.910
which can then be put into the string

98
00:04:38.910 --> 00:04:41.800
that we're building here using the template literal.

99
00:04:41.800 --> 00:04:42.663
So let's say,

100
00:04:43.790 --> 00:04:45.970
just to do some calculations here.

101
00:04:45.970 --> 00:04:49.043
So 2037 minus 1991,

102
00:04:50.020 --> 00:04:51.990
years old.

103
00:04:51.990 --> 00:04:55.023
So again, this is an expression here.

104
00:04:56.000 --> 00:04:57.563
And so this one will work.

105
00:04:58.440 --> 00:05:00.823
However, we cannot do this.

106
00:05:01.860 --> 00:05:03.710
For example, we could not insert

107
00:05:04.870 --> 00:05:07.340
this if statement in here.

108
00:05:07.340 --> 00:05:10.963
It wouldn't make any sense at all, okay?

109
00:05:12.140 --> 00:05:13.380
Let's try that.

110
00:05:13.380 --> 00:05:15.270
And so we actually get right away,

111
00:05:15.270 --> 00:05:17.880
unexpected token, if.

112
00:05:17.880 --> 00:05:21.960
And that's because JavaScript knows that this is a statement

113
00:05:21.960 --> 00:05:24.320
and statements don't make sense

114
00:05:24.320 --> 00:05:27.103
where JavaScript expects an expression.

115
00:05:29.600 --> 00:05:31.773
Now, if we had a variable, for example,

116
00:05:33.000 --> 00:05:35.350
then if we used that here,

117
00:05:35.350 --> 00:05:38.710
then this would actually also be an expression.

118
00:05:38.710 --> 00:05:39.890
Because this variable

119
00:05:39.890 --> 00:05:43.710
will essentially just be replaced with this string.

120
00:05:43.710 --> 00:05:45.430
And so this produces a value,

121
00:05:45.430 --> 00:05:48.883
and so of course this is completely acceptable.

122
00:05:50.820 --> 00:05:53.760
Okay, and that's actually all I had to show you

123
00:05:53.760 --> 00:05:55.410
in this video.

124
00:05:55.410 --> 00:05:57.990
Again, my goal was really just to make sure

125
00:05:57.990 --> 00:05:59.830
that you kind of understand

126
00:05:59.830 --> 00:06:02.610
the difference between expressions and declarations

127
00:06:02.610 --> 00:06:04.080
so that moving forward,

128
00:06:04.080 --> 00:06:06.423
we are really both on the same page here.

