711

I know in PHP we can do something like this:

$hello = "foo";
$my_string = "I pity the $hello";

Output: "I pity the foo"

I was wondering if this same thing is possible in JavaScript as well. Using variables inside strings without using concatenation — it looks more concise and elegant to write.

1
  • Either with a simple RegEx replace, or something more advanced, like custom-string-formatter library. Commented Aug 15 at 16:35

18 Answers 18

1287

You can take advantage of Template Literals and use this syntax:

`String text ${expression}`

Template literals are enclosed by the back-tick (` `) (grave accent) instead of double or single quotes.

This feature has been introduced in ES2015 (ES6).

Example

var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b}.`);
// "Fifteen is 15.

How neat is that?

Bonus:

It also allows for multi-line strings in javascript without escaping, which is great for templates:

return `
    <div class="${foo}">
         ...
    </div>
`;

Browser support:

As this syntax is not supported by older browsers (mostly Internet Explorer), you may want to use Babel/Webpack to transpile your code into ES5 to ensure it will run everywhere.


Side note:

Starting from IE8+ you can use basic string formatting inside console.log:

console.log('%s is %d.', 'Fifteen', 15);
// Fifteen is 15.
Sign up to request clarification or add additional context in comments.

6 Comments

Don't miss the fact that the template string is delimited with back ticks (`) instead of your normal quote characters. "${foo}" is literally ${foo} `${foo}` is what you actually want
Also there are many transpilers to turn ES6 into ES5 to fixed the compatibility issue!
when i change a or b value. console.log(Fifteen is ${a + b}.); does not changed dynamically. it always shows Fifteen is 15.
Back ticks are a life savior.
But the issue is when I am using this inside a php file then $variable will be considered as php variable and not a js variable as php variable are in format $variable_name.
|
177

Prior to Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge, nope, that was not possible in javascript. You would have to resort to:

var hello = "foo";
var my_string = "I pity the " + hello;

2 Comments

It will soon be possible in javascript (ES6) with Template Strings, see my detailed answer below.
It is possible if you like to write CoffeeScript, which actually IS javascript with a better syntax.
44

well you could do this, but it's not esp general

'I pity the $fool'.replace('$fool', 'fool')

You could easily write a function that does this intelligently if you really needed to

3 Comments

Pretty decent, actually.
This answer is good when you need to store template string in database and process it on demand
Good one, it works well. very simple but didn't come on mind.
44

Prior to Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge, no. Although you could try sprintf for JavaScript to get halfway there:

var hello = "foo";
var my_string = sprintf("I pity the %s", hello);

1 Comment

Thanks. If you're using dojo, sprintf is available as a module: bill.dojotoolkit.org/api/1.9/dojox/string/sprintf
31

Complete and ready to be used answer if lacking ES6:

 var Strings = {
        create : (function() {
                var regexp = /#{([^{]+)}/g;

                return function(str, o) {
                     return str.replace(regexp, function(ignore, key){
                           return (key = o[key]) == null ? '' : key;
                     });
                }
        })()
};

Call as

Strings.create("My firstname is #{first}, my last name is #{last}", {first:'Neo', last:'Andersson'});

To attach it to String.prototype:

String.prototype.create = function(o) {
           return Strings.create(this, o);
}

Then use as :

"My firstname is #{first}".create({first:'Neo'});

If you are on >ES6 then you can also do:

let first = 'Neo'; 
`My firstname is ${first}`; 

3 Comments

The problem I find with My name is ${first} is that it is evaluated at the time you declare it, so from what I have been able to find, there is no way to save it as a string to be evaluated later of declaring the string. If I am wrong, please mention how it can be achieved with template literals
There are ways, but I am not sure I understand your environment. You would have to recreate with ES6, but with my solution not so. You could store it aside and but would have to call create({ ... }) on it.
Yes, sorry, my comment is not understandable because I was trying to put the string inside backticks ` to indicate that I wanted to use template literals. But I didn't find any way to use it without evaluating it at the same time, so the solution with create is good as an alternative.
12

2022 update: Just use the ES6 Template Literals feature. It's fully supported in practically every browser you'll encounter these days. If you are still targeting browsers like IE11 and lower .. well, my heart goes out to you. The below solution I came up with 5 years ago will still work for you. Also, email me if you want a job that doesn't involve catering to old browsers 👍.

You can use this javascript function to do this sort of templating. No need to include an entire library.

function createStringFromTemplate(template, variables) {
    return template.replace(new RegExp("\{([^\{]+)\}", "g"), function(_unused, varName){
        return variables[varName];
    });
}

createStringFromTemplate(
    "I would like to receive email updates from {list_name} {var1} {var2} {var3}.",
    {
        list_name : "this store",
        var1      : "FOO",
        var2      : "BAR",
        var3      : "BAZ"
    }
);

Output: "I would like to receive email updates from this store FOO BAR BAZ."

Using a function as an argument to the String.replace() function was part of the ECMAScript v3 spec. See this SO answer for more details.

3 Comments

Is this efficient?
The efficiency will depend largely on the user's browser, since this solution delegates the "heavy lifting" of matching the regex and doing string replacements to the browser's native functions. In any case, since this is happening on the browser-side anyway, efficiency is not such a huge concern. If you want server-side templating (for Node.JS or the like) you should use the ES6 template literals solution described by @bformet, as it is likely more efficient.
ES6 Template Literals are limited to JavaScript code execution. In a lot of cases the template comes from a file or HTTP or even user input, for all of which the Template Literals are not usable.
10

If you like to write CoffeeScript you could do:

hello = "foo"
my_string = "I pity the #{hello}"

CoffeeScript actually IS javascript, but with a much better syntax.

For an overview of CoffeeScript check this beginner's guide.

Comments

9

I would use the back-tick ``.

let name1 = 'Geoffrey';
let msg1 = `Hello ${name1}`;
console.log(msg1); // 'Hello Geoffrey'

But if you don't know name1 when you create msg1.

For exemple if msg1 came from an API.

You can use :

let name2 = 'Geoffrey';
let msg2 = 'Hello ${name2}';
console.log(msg2); // 'Hello ${name2}'

const regexp = /\${([^{]+)}/g;
let result = msg2.replace(regexp, function(ignore, key){
    return eval(key);
});
console.log(result); // 'Hello Geoffrey'

It will replace ${name2} with his value.

Comments

6

I wrote this npm package stringinject https://www.npmjs.com/package/stringinject which allows you to do the following

var string = stringInject("this is a {0} string for {1}", ["test", "stringInject"]);

which will replace the {0} and {1} with the array items and return the following string

"this is a test string for stringInject"

or you could replace placeholders with object keys and values like so:

var str = stringInject("My username is {username} on {platform}", { username: "tjcafferkey", platform: "GitHub" });

"My username is tjcafferkey on Github" 

Comments

4

If you're trying to do interpolation for microtemplating, I like Mustache.js for that purpose.

Comments

4

Don't see any external libraries mentioned here, but Lodash has _.template(),

https://lodash.com/docs/4.17.10#template

If you're already making use of the library it's worth checking out, and if you're not making use of Lodash you can always cherry pick methods from npm npm install lodash.template so you can cut down overhead.

Simplest form -

var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'

There are a bunch of configuration options also -

_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'

I found custom delimiters most interesting.

Comments

3

Simply use:

var util = require('util');

var value = 15;
var s = util.format("The variable value is: %s", value)

Comments

1
let data = Hello User
console.log(`<a href=''><i class='fa-solid fa-star'></i>${data}</a>`)

output => <a href=''><i class='fa-solid fa-star'></i>Hello User</a>

note use `` don't use "" or '' it will not work

Comments

0

Create a method similar to String.format() of Java

StringJoin=(s, r=[])=>{
  r.map((v,i)=>{
    s = s.replace('%'+(i+1),v)
  })
return s
}

use

console.log(StringJoin('I can %1 a %2',['create','method'])) //output: 'I can create a method'

Comments

0

Peace quote of 2020:

Console.WriteLine("I {0} JavaScript!", ">:D<");

console.log(`I ${'>:D<'} C#`)

Comments

0

Maybe

wrt=(s, arr=[])=>{
    arr.map((v,i)=>{s = s.replace(/\?/,v);});
    return s;
};
a='first var';
b='secondvar';
tr=wrt('<tr><td>?<td></td>?</td><tr>',[a,b]);
console.log(tr);
//or use tr in html(tr), append(tr) so on and so forth
// Use ? with care in s

Comments

-1
String.prototype.interpole = function () {
    var c=0, txt=this;
    while (txt.search(/{var}/g) > 0){
        txt = txt.replace(/{var}/, arguments[c]);
        c++;
    }
    return txt;
}

Uso:

var hello = "foo";
var my_string = "I pity the {var}".interpole(hello);
//resultado "I pity the foo"

Comments

-5

var hello = "foo";

var my_string ="I pity the";

console.log(my_string, hello)

1 Comment

That doesn't answer the question. You might log out both strings in one line, but that doesn't give you a new string containing both strings, which is what OP is asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.