RPGMaker MV ES6 Part 3: Template Literals

Introduction

Today we discuss template literals, which are a new type of string in JavaScript. Compared to the other string types, template literals are more robust. Here are some of the features of template literals.

Features

  • Multiline strings
  • Easy syntax for passing variables
  • Expression Interpolation

In this post, we’ll be going through each feature in code snippets.

Multiline Strings

Unlike regular strings in JavaScript, which suffer from being a single line, template literals support multiline strings, which means you can actually have formatted text in a template literal. This is an extremely powerful feature because it allows developers to construct complicated strings in RPGMaker MV. This means you can create formatted text strings in the code that can be displayed to the user without having to use linebreaks, tabs, spaces; it adds a lot of cleanliness. Which brings me to the next point, expression interpolation.

Expression Interpolation

Expression interpolation could be considered a fancy word for passing variables to a template literal or a string. We could always do this in JavaScript before using the “+” symbol. The plus symbol allowed us to concatenate our variable with a string no problem. However, now, we have a simpler way to pass variables into a string, that looks a lot cleaner, and is much easier to read. 

You’ll see the power of both in the code example below, with a comparison of what it’s like in ES5 vs ES6.

Code Example

As you can see, the ES6 syntax is a lot cleaner and easier to use when you to include multiple lines in a single string or variables. One thing it definitely increases is code readability. If you wanted to use the ES5 syntax, you’d have to insert line breaks, if the string is too long, you have to concatenate — in short too unwieldy.

 

Conclusion

With that said, I hope this post helps you improve your code, and if you have any suggestions please comment down below!

Leave a Reply

Your email address will not be published.