Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpolate variables in strings in JavaScript, without concatenation?

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.

like image 772
DMin Avatar asked Jul 21 '10 21:07

DMin


People also ask

How do you interpolate a string in JavaScript?

Use a template literal to interpolate a variable in a string, e.g hello ${myVar} . Template literals are delimited with backticks and allow us to embed variables and expressions using the dollar sign and curly braces ${expression} syntax. Copied! const name = 'Tom'; const result = `Hello, ${name}!`; console.

Does JavaScript support string interpolation?

In JavaScript, the template string implements the string interpolation. A template string is defined by wrapping a sequence of characters into a pair of backticks `I'm template string` . The template string placeholders have the format ${expression} , for example `The number is ${number}` .

What is the difference between string interpolation and concatenation?

Concatenation allows you to combine to strings together and it only works on two strings. Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable.

How do you embed variables expressions in a string?

Variables in Strings with Template Literals One special feature of the template literal feature is the ability to include expressions and variables within a string. Instead of having to use concatenation, we can use the ${} syntax to insert a variable.


2 Answers

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. 
like image 146
bformet Avatar answered Oct 21 '22 10:10

bformet


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; 
like image 45
Sarfraz Avatar answered Oct 21 '22 12:10

Sarfraz