Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
00:00.300 --> 00:05.190
In this video I want to take a quick moment to talk about template strings in Javascript.
00:05.190 --> 00:09.450
So we're going to step away from functions for just a second and we're going to talk about this new
00:09.450 --> 00:11.320
way to create strings.
00:11.370 --> 00:17.280
Right now every time we've created a string we've used single quotes we put our string characters inside
00:17.310 --> 00:21.260
like we do here here and here and then we see that in the result.
00:21.270 --> 00:24.590
So in this case I add all of this stuff up concatenating them on to each other.
00:24.720 --> 00:31.130
And the end result is the message that prints with the user's name and their score inside of this string.
00:31.440 --> 00:35.820
Now what we're going to do is explore the template straight as the name suggests it's a way to create
00:35.850 --> 00:37.300
a template string.
00:37.440 --> 00:41.580
So there are parts of this template string that are going to be static and there are other parts that
00:41.580 --> 00:43.140
are going to be dynamic.
00:43.260 --> 00:49.080
From a functional standpoint it's going to do exactly what we already do here but the syntax is going
00:49.080 --> 00:51.540
to make it a lot easier to work with.
00:51.540 --> 00:56.430
So let's go ahead and explore this syntax Double-O cancelled that lag.
00:56.550 --> 01:00.700
And we're gonna make a regular string the string Andrew using single quotes.
01:00.700 --> 01:02.660
This is a regular boring old string.
01:02.670 --> 01:05.140
It's something that we've done plenty of times before.
01:05.160 --> 01:06.940
I'm not even going to run the program.
01:06.960 --> 01:11.850
What I want to talk about though before I talk about template strings is that we can create regular
01:11.860 --> 01:14.910
strings using double quotes as well.
01:14.910 --> 01:19.630
So right here I can use a double quote and I can type my message inside.
01:19.680 --> 01:22.500
There is no difference between the two in javascript.
01:22.560 --> 01:27.820
I used single quotes because I don't have to hit the shift key so it's just one less Kieta press.
01:27.960 --> 01:29.960
I also think they look a bit cleaner.
01:29.970 --> 01:34.300
You can also use double quotes for all of your strings that would work as well.
01:34.320 --> 01:39.690
I just don't recommend using a combination of the two find which one works best for you and stick with
01:39.690 --> 01:41.320
it for me in this course.
01:41.370 --> 01:44.970
I'm always going to use single quotes for my regular strings.
01:44.970 --> 01:48.120
I did just want to make you aware that this exists.
01:48.120 --> 01:49.530
It's a regular string.
01:49.590 --> 01:55.960
I could even concatenate something onto it plus followed by a single quoted string Mead.
01:56.010 --> 01:59.600
So the end result would be an jimmied with no space in between.
01:59.610 --> 02:02.150
It's perfectly fine to mix the two like this.
02:02.280 --> 02:07.480
But as I mentioned for clarity's sake I recommend picking one and sticking with it.
02:07.590 --> 02:11.190
The only thing we can't do is open and close with a different quote.
02:11.250 --> 02:15.150
So I can't open Meade with a single quote and trying to close it with a double quote.
02:15.150 --> 02:16.290
That's not valid.
02:16.320 --> 02:20.770
If I open it with single or double I have to close it with the same.
02:20.790 --> 02:22.290
So these are all just regular strings.
02:22.290 --> 02:23.350
Nothing new there.
02:23.370 --> 02:26.500
I just wanted to make you aware of the double quote syntax.
02:26.670 --> 02:32.670
What we're going to explore now is the template string which is indeed a bit different to create a template
02:32.670 --> 02:33.340
string.
02:33.360 --> 02:36.320
We use the back tick key on your keyboard.
02:36.330 --> 02:41.310
This is located to the left of the one key and it looks very similar to a single quote.
02:41.340 --> 02:44.610
But as you'll notice it is on a slight angle.
02:44.620 --> 02:49.770
Now here we can put anything we can put in a regular string so I can just start typing the characters
02:49.770 --> 02:50.970
that I want to use.
02:50.970 --> 02:59.190
I could say hey my name is followed by my name with an exclamation mark that's going to print the static
02:59.190 --> 03:02.100
string to the screen every single time we run the program.
03:02.400 --> 03:08.780
Now let's say we had name above let name equal and Jeru and we want to use it in the string.
03:09.000 --> 03:11.720
Let's go ahead and actually switch the name up to something else.
03:11.740 --> 03:13.520
I'll go ahead and use jet.
03:13.560 --> 03:17.650
We want to put it right here although we want to leave everything before.
03:17.670 --> 03:22.060
Hey my name is and everything after the exclamation mark alone.
03:22.200 --> 03:27.190
So what do we do well with regular strings we would have to use string concatenation.
03:27.210 --> 03:28.380
I would have one string.
03:28.410 --> 03:34.470
The first part that I would add the concatenation operator to tack on name that I would add on the concatenation
03:34.470 --> 03:39.210
operator again to tack on the exclamation mark for template strings.
03:39.300 --> 03:44.790
We just moved to the part of the string where we want to inject the value and we use the following syntax.
03:44.790 --> 03:50.540
It's a dollar sign followed by the opening and closing curly brace inside.
03:50.820 --> 03:52.960
We can add a jobless script to expression.
03:53.040 --> 03:56.870
This is where we pick what we want to put in this part of the template string.
03:56.910 --> 04:01.240
In this case we want to put whatever is stored in the name variable.
04:01.320 --> 04:05.980
So the end result of the stuff highlighted is going to be the string.
04:06.000 --> 04:11.130
Jan the dollar sign and the curly brace do not make it into the final version.
04:11.130 --> 04:14.970
This is just a syntax for injecting some sort of value.
04:14.970 --> 04:19.400
Let's go ahead and check this out by actually running the arguments file over here.
04:19.780 --> 04:22.160
Node arguments J.
04:22.170 --> 04:22.550
As.
04:22.590 --> 04:23.310
When I run it.
04:23.310 --> 04:24.210
What do I get I get.
04:24.210 --> 04:25.930
Hey my name is Jen.
04:26.010 --> 04:34.410
If I change the name to something else Hey my name is victim and I rerun the script.
04:34.410 --> 04:35.560
That is exactly what I get.
04:35.580 --> 04:41.550
Hey my name is Vikram because it's using the new value for the name variable in this particular part
04:41.730 --> 04:43.430
of the template string.
04:43.770 --> 04:47.600
Now we could add as many values as we need to into this string.
04:47.610 --> 04:57.300
You could say something like I am 12 years old or whatever their age is and this could also come from
04:57.300 --> 04:58.120
a variable.
04:58.170 --> 04:59.890
So I would add age right here.
05:01.030 --> 05:07.120
At age 12 then I would add the exact same syntax dollar sign.
05:07.120 --> 05:09.380
Curly braces inside of there.
05:09.370 --> 05:12.660
The thing I want to inject in this case age.
05:12.730 --> 05:16.690
Now we have a template string with two dynamic values inside of it.
05:16.690 --> 05:20.690
I can go ahead and rerun it and I get the full message.
05:20.890 --> 05:22.310
So what's the real advantage.
05:22.360 --> 05:28.720
And the answer is it's a syntactical advantage so I can do the exact same thing I do here with regular
05:28.720 --> 05:31.160
old strings and the plus operator.
05:31.210 --> 05:37.060
The difference is that in my opinion and in the opinion of a lot of others this is way harder to read
05:37.120 --> 05:38.800
than this down below.
05:38.860 --> 05:42.030
This is also way harder to change.
05:42.130 --> 05:47.830
So let's say I want to move name to the front of the string or I want to take this part of the message
05:48.040 --> 05:49.020
and move it to the end.
05:49.030 --> 05:50.700
Putting score in the beginning.
05:50.980 --> 05:57.370
I have to rearrange things and also rearrange the plus operators making sure to remove the leading and
05:57.370 --> 05:59.830
trailing spaces around appropriately.
05:59.860 --> 06:05.140
It becomes quite a bit of a headache with template Shearing's it's really easy to rearrange things if
06:05.140 --> 06:08.330
I want to move the first sentence after the second sentence.
06:08.350 --> 06:10.510
I can just go ahead and cut it out.
06:10.810 --> 06:15.630
I can remove that one space I could add a space here and I can paste it in.
06:15.700 --> 06:20.540
So with template strings it's way easier to actually work with them using concatenation.
06:20.560 --> 06:21.940
Yes we can get the job done.
06:21.970 --> 06:25.660
It's just not as ideal as working with template strings.
06:25.960 --> 06:30.070
So since we know a better way to do things let's go ahead and actually use it.
06:30.070 --> 06:34.700
I'm going to replace the string here with a template string.
06:34.930 --> 06:36.160
So what are we going to do.
06:36.160 --> 06:42.280
I'm going to return and I'm going to create a template string using the back tick which automatically
06:42.280 --> 06:44.850
gets closed for me in year.
06:44.860 --> 06:52.120
I'm going to put everything I want so its name call and space then we are going to inject the name so
06:52.120 --> 06:55.360
dollar sign at curly braces name.
06:55.360 --> 06:56.520
Then after name.
06:56.530 --> 07:01.410
I'm going to add a space hyphen space score colon.
07:01.420 --> 07:06.680
Then I'm going to inject at the very end the actual score variable.
07:06.760 --> 07:11.000
So this and what I have down below are going to produce the exact same output.
07:11.020 --> 07:15.580
I just find this in much more readable and way more maintainable.
07:15.580 --> 07:20.470
Let's go out and prove that it actually still prints the exact same result by saving the file and rerunning
07:20.470 --> 07:22.120
things the last time we ran it.
07:22.120 --> 07:23.450
We got this message.
07:23.710 --> 07:30.940
If I rerun it again we get the exact same message name Anonymous score ninety nine template strings.
07:31.000 --> 07:34.300
Once again do not really give us any new functionality.
07:34.390 --> 07:36.610
It just gives us a better way to get things done.
07:36.610 --> 07:39.930
It's something we're going to be using extensively throughout the course.
07:39.970 --> 07:43.210
I'm going to remove the regular string return statement and down below.
07:43.210 --> 07:48.070
I'm also going to delete the example we set up so we now have an example right here.
07:48.310 --> 07:54.760
Your challenge for the video is going to be to integrate a template string into the get tip function
07:54.760 --> 07:55.590
down below.
07:55.690 --> 07:57.850
So let's go ahead and talk about what I'd like you to do.
07:57.850 --> 08:04.090
Your job is to return a string a little message from get temp as opposed to returning just the actual
08:04.150 --> 08:09.420
tip value and the string you're going to return is going to be created from a template string.
08:09.610 --> 08:11.520
So let's go ahead and explore this right here.
08:11.530 --> 08:14.950
The message for this data would look like the following.
08:14.980 --> 08:25.360
A 25 percent tip on $40 the value passed in would be and the actual calculation would be 10.
08:25.390 --> 08:28.770
So instead of just returning the number 10 I am including the number 10.
08:28.780 --> 08:31.180
In the larger template string.
08:31.420 --> 08:32.870
Now there are three values here.
08:32.890 --> 08:35.040
We have 2005 to calculate this.
08:35.050 --> 08:38.400
You just take the top percent and multiply it by 100.
08:38.410 --> 08:44.670
We have 40 which is just passed in directly and we have 10 which comes from the calculation right here.
08:44.830 --> 08:48.800
It's going to be your job to put all of this together inside of a template string.
08:48.880 --> 08:53.620
You can create a couple of variables beforehand to run those calculations and then you can return the
08:53.620 --> 08:57.890
template string much like we return a template string up above.
08:57.910 --> 09:03.070
Take as much time as you need to get this one done and then mess around with it tried to pass in a few
09:03.070 --> 09:08.500
different values and make sure you do see the correct message for each pause the video and when you're
09:08.500 --> 09:10.930
done go ahead and click play
09:14.080 --> 09:18.220
all right how that one go hopefully you were able to get things done without too much trouble.
09:18.220 --> 09:21.520
I want to go ahead and kick things off by creating a few variables.
09:21.520 --> 09:26.000
First up let percent equal.
09:26.170 --> 09:32.470
And here I'm just going to take the tip present and I'm going to multiply it by 100.
09:32.470 --> 09:37.460
That's going to convert point two over to 20 which I need for this first part.
09:37.570 --> 09:39.910
Now for the second part you don't have to do anything.
09:39.910 --> 09:42.780
I had that number passed in directly for the third part.
09:42.790 --> 09:44.460
I do want to calculate the tip.
09:44.470 --> 09:52.960
So let tip equal the total times the tip per cent that is going to give me in the case of this data
09:53.200 --> 09:55.000
a value of 10.
09:55.000 --> 09:58.690
And then finally I'm going to return the template string.
09:58.720 --> 10:04.030
So right here let's go ahead and open and close some back tax and we're gonna start filling things out.
10:04.030 --> 10:10.830
So a space followed by the percent value from up above a dollar sign curly braces.
10:10.880 --> 10:12.530
Percent.
10:12.900 --> 10:17.620
And then after that space actually not space we're going to use the percent sign.
10:17.830 --> 10:22.120
Then a space tip on space dollar sign.
10:22.120 --> 10:23.600
And this is where we put the total.
10:23.650 --> 10:27.560
So dollar sign curly braces total.
10:27.760 --> 10:32.400
Then after that total space would be space.
10:32.530 --> 10:37.300
And then finally we wrap up with a dollar sign and then the actual calculated temp.
10:37.420 --> 10:41.560
So dollar sign curly braces right inside of your tip.
10:41.560 --> 10:44.990
Now we can actually test things out by rerunning the file.
10:45.040 --> 10:51.820
I'm going to save my work rerun things from the terminal and I get a 25 percent tip on 40 bucks would
10:51.820 --> 10:53.410
be ten dollars.
10:53.410 --> 10:58.930
Everything is working well and we were able to set up this string using template strings.
10:58.930 --> 11:01.500
Now let's go ahead and switch up the values.
11:01.570 --> 11:08.890
I'm going to switch to the default 20 percent tip and I'm going to switch the number over to 50 if I
11:08.890 --> 11:10.470
rerun things this time around.
11:10.480 --> 11:12.070
I'd actually still get 10.
11:12.160 --> 11:19.330
Let me switch this over to 60 to see a different value print area run it and I get a 20 percent tip
11:19.330 --> 11:23.290
on 60 bucks would be $12 which is correct.
11:23.290 --> 11:23.850
All right.
11:23.860 --> 11:28.200
Now that we have this in place I want to show you one alternative way you could have gotten this done.
11:28.360 --> 11:32.930
You can actually run these calculations right inside of the curly braces.
11:32.950 --> 11:36.850
So if I wanted to I could calculate tip percent percent
11:39.300 --> 11:44.140
Time's 100 right here I would do the exact same thing for the tip.
11:44.280 --> 11:51.950
I could multiply the total times the tip per cent to get the actual total tip.
11:51.960 --> 11:55.530
This would allow us to remove these two variables completely.
11:55.530 --> 11:59.330
Now this is not necessarily what you want to do in all situations.
11:59.340 --> 12:01.950
It's just a different way to solve the problem.
12:02.100 --> 12:08.820
If I were to save things and rerun things we get the exact same message the calculations are still being
12:08.850 --> 12:13.050
executed at the same they're just now embedded right in the template string.
12:13.320 --> 12:17.880
There's going to be good in some situations but it can also create situations where the template sharing
12:17.880 --> 12:23.130
is now much easier to read through since it includes more code and math operations.
12:23.130 --> 12:29.040
So I'm going to actually undo those changes and bring it back to the example I had before where I simply
12:29.100 --> 12:35.220
reference the percent variable right here and the tip variable right here.
12:35.250 --> 12:38.040
This is all about personal preference and style.
12:38.040 --> 12:43.440
I prefer to keep my template strings cleaner just referencing variables as opposed to trying to run
12:43.440 --> 12:48.060
calculations right inside creating a situation that's just a bit messier.
12:48.060 --> 12:48.520
All right.
12:48.540 --> 12:50.100
That is it for this one.
12:50.100 --> 12:54.540
Now that we know how to use template strings we can use them throughout the course to create more meaningful
12:54.570 --> 12:55.560
messages.
12:55.560 --> 12:56.960
I'm excited to continue on.
12:56.970 --> 12:58.330
I'll see you next time.
17448
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.