Transcript
00:00 So let's get started with this. We're gonna log the answer is 42, but we wanna use a template literal that uses an expression. So here we're gonna have it be 40 plus two, and we're gonna interpolate that in our template literal. So here we say console log with a back tick that starts our string, and then inside of here is the string. And then we have this special dollar sign curly brace.
00:25 That is special syntax that says, hey. I'm not a string anymore. I am an expression. And that expression for us is 40 plus two. And then, we close that expression with a closing curly brace.
00:37 And then we could have more string stuff here, but, yeah, maybe we'll we'll throw an exclamation point in there just for the fun of it. And, and then we close with that closing back tick. And we also, can do hello typescript using a template literal. So you don't have to have interpolation. It still works as a regular string.
00:56 And here we can do, additional math stuff. 10 times five equals 10 times five. Tada. And if those were variables, you could, interpolate variables in there and stuff. We'll get into variables later.
01:09 But this, gives you an idea. And here we can do quotes, hello, TypeScript, or, hello, TypeScript with a single quote. Now you don't have to worry about it. Although, you do have the other problem. Now what if I wanted to actually show a backtick in my output?
01:24 Well, you can actually escape that as well. So now you can have backticks, but we don't use backticks quite as much. And so, it is nice to be able to just use the backtick. The other cool thing you can do with this is multi lines. So ta da.
01:39 We've got multi lines here. Let's run that. And there it is. Everything between this opening back tick and the closing one is included in the string. That includes not only these new line characters, but also, if you can tell, these tab characters as well.
01:53 And so, though it's kinda hard to see in this output, but, yeah, those tab characters are also present. And we can say whatever and boom, there it is. And that works just nicely. So, you get new lines. You get interpolation.
02:10 So you don't have to worry about concatenation as much. You don't have to worry about, escaping single quote or double quote. So template literals, they're pretty cool. And there's really no problem, which is using them anywhere everywhere. I personally don't like doing that.
02:28 Just maybe old habits die hard. I'm, been doing this a long time, and, I just prefer using SQL quotes when I can. So that's what I do. But, yeah, if you wanna use backticks, I cannot stop you. I I can't argue against you either.
02:42 It's be a perfectly fine way to develop software. You will probably, as you're generating, your code with AI and reviewing it, most of the time, the AI is probably going to use either single or double quotes and only use, template literals when that's necessary. There's also, another feature of template literals that gets a little bit more advanced than we're going to get in, into here, but, you might see it sometimes. You'll see, like, SQL that shows up right before the back tick, or you might see a, fetch or a, get or something like that. This is actually a special type of function that takes the template literal in, and its interpolated values as special arguments.
03:26 Like I said, pretty advanced TypeScript and and JavaScript related stuff there, so we're not going to get into that. But, yeah, it is kind of an interesting thing. So if you ever see something like that, you can know, oh, that's a special feature of template literals, and then you can go look up, like, what is that thing? So So that's called a tagged template literal. So you can go look that up later if you're interested.
03:47 And with that, I think you know what you need to know about template literals. Good job.
