at. at. -4 years ago 73
Javascript Question

How to insert variables in JavaScript strings?

What's the best way to do insert variables in a string in JavaScript? I'm guessing it's not this:

var coordinates = "x: " + x + ", y: " + y;

In Java,
s are immutable and doing something like the above would unnecessarily create and throw away
s. Ruby is similar and has a nice way of doing the above:

coordinates = "x: #{x}, y: #{y}"

Does something similar exist for JavaScript?

Answer Source

Not sure why this question was being downvoted, Looks like me and thousands of others searched it. After a little bit of looking I found exactly what I wanted. Introduced in ES5 as "template strings"

MDN docs:

const name = "Nick"
const greeting = `Hello ${name}`  // "Hello Nick"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download