Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Use Template Literals in ES6

    John LindquistJohn Lindquist

    ECMAscript 6 lets us use string templates to gain a lot more control over strings in JavaScript.

    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Man 1: 00:00 If you have ever worked with strings in JavaScript, you've most likely concatenated two strings by just saying the variable than plus and then adding a string to it, and then when you run it, you just get hello world, all put together.

    00:12 ES6 allows you to put your variables inside of your string. I'm going to surround this with a inaudible . So I'll put one there and I'll put one here.

    00:21 Then, instead of doing plus and then quote, I'll just surround this guy with the dollar sign, curly brace, and then close curly brace. You'll see if I rerun this, I'll get hello world.

    00:35 It actually maintained this space because I forgot to delete it. So if I delete that and run it again, then we'll get hello world without the space.

    00:43 It actually respects white space even across multiple lines. If I put some lines in here and if I say "hello," tab, tab, "world," and run this. You'll see I get some blank lines, hello all the way at the left and then a couple tabs, and then world, and then more blank lines.

    01:00 I could pull off things like hello you crazy world, how are you, and rerun this and you can see, we can manipulate strings however we'd like.

    01:17 If I want to extract any of these into variables, I could just instead of world here, I could just do place and then I can do place. We'll just say "planet," and rerun this, and you can see we get hello, you crazy planet, all the way out here.

    01:36 It's also worth noting that you can do expressions inside of these braces. If you want to do X + Y, then just show Y and just show X, and then run this. You can see we get 1 + 2 = 3, and then this is one.

    01:50 There's this string plus, there's two, there's the string equals, and then X + Y is the expression.

    01:58 Lastly, is a basic introduction to tagging these string templates that I have. It's and then the hour of the day, and then, I'm sleepy. If I run this, you'll see it's 15 or 3 o'clock, and that I'm sleepy.

    02:12 I don't get sleepy until after 20, so all I have here is this and how am I going to make this into a variable? Well, I can actually parse out this message and get this value and change this string based on this value.

    02:27 I'm going to create a function called, "tag," and notice I don't put any sort of parameters or anything around this. I just type the function name.

    02:36 I'll say, "Function tag," you can name it whatever you want, tag or parse, or whatever, and then it takes these strings and the values, these strings being an array.

    02:49 We'll log this out. The array of it's and I'm sleepy are the two strings. This piece and this piece, and then the values are the values found in here.

    03:04 When I log this out, this will be an array with just 15 in it. So switch this over to a value. We'll just make it an empty value.

    03:13 Then, we'll say, if the first value, so value 0is less than 20. I'll assign the second value and say this should be "I'm awake."

    03:28 Then, we will turn a new string. So two back ticks, and we'll just say, strings zero values, zero, strings one, and values one.

    03:48 Then, once we log this out, you can see it says, "It's 15, I'm awake," because 15 is less than 20 and we assign this second value which is this guy to awake.

    04:04 Now, there's much more advanced stuff you can do with this such as parsing HTML and using RegEx and everything, but we're going to end right there for right now.

    04:12

    Discuss

    Discuss