Advanced: Template Strings
What are Template Strings?
Template strings, also commonly known as template literals, are a cool feature in JavaScript that makes it easier to work with strings. You know, those pieces of text that you use in your code to say "Engage!" or to show your friend's name? Template strings help you combine text with variables in a more convenient way.
How Do We Use Template Strings?
To create a template string, you use backticks (`
) instead of single ('
) or double ("
) quotes. Backticks are usually found on the same key as the tilde (~
) on your keyboard.
Why Are Template Strings Better?
Well, imagine you have a crew member named Data, and you want to say hi to him using his name. Without template strings, you'd have to do something like this:
let name = "Data";
let greeting = "Hello, Commander " + name + "!";
But with template strings, you can use placeholders to make your life easier. Placeholders are created using ${}
with the variable inside:
let name = "Data";
let greeting = `Hello, Commander ${name}!`;
See how clean and simple it is? You don't need to use the + sign any more to combine text and variables.
More Cool Stuff
Template strings also let you do other neat things:
Multiline strings
You can create strings that span multiple lines just by pressing Enter. No need to use \n
any more!
let multiline = `Captain's Log,
Stardate 43125.8,
We have entered a new region of space.`;
Expressions
You can even do math or other calculations inside the placeholders:
let warpFactor = 9;
let speed = 2;
let result = `Warp factor ${warpFactor} is ${warpFactor * speed} times faster than warp factor ${speed}.`;
Special characters
Template strings make it easy to include special characters in your text, like quotes or backslashes, without the need for escape characters:
let quote = `Captain Picard once said, "Make it so!"`;
let filePath = `C:\StarTrek\TNG`;
Recap
Template strings are a great way to work with strings in JavaScript. They make it easy to combine text and variables using placeholders, create multiline strings, perform calculations, and include special characters. Just remember to use backticks (`
) instead of single or double quotes, and you'll be boldly going where no coder has gone before!
Further Reading
Click here to read even more about template strings!