Rewriting Javascript: Using Template Strings

Chris Burgin
3 min readApr 14, 2017
TLDR; See Banner Image;

The Scenario

Sometimes you need to get data from an object and convert that data into a user friendly string. Its a simple problem, with a simple solution, but maybe we can improve it slightly.

Lets say we have the following JSON object. Its a simple user and want to create a string that advertises that users twitter account.

const user = {
first: "Chris",
last: "Burgin",
twitter: "@chrisburgin95"
}

Now lets say we want to take the information from this user object and convert into a nice message for our users that has the following format.

{first} {last} can be found on twitter at {twitter}

The Old Way

This is fairly easy to generate this string using the following code. I think we have all seen/done this before.

var twitterMessage = user.first + " " + user.last + " can be found   on twitter at " + user.twitterconsole.log(twitterMessage)

This method words, and it works fairly well. But there is a better, more functional and reusable way to generate this string.

The New Way

Im going to break down each change one step at a time. So stick with me if you get bored.

--

--

Chris Burgin

Christ follower. Dog caretaker. Developer at Generation Tux.