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.
Answer from bformet on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
If you put a number in quotes, ... a text string. const pi = 3.14; let person = "John Doe"; let answer = 'Yes I am!'; Try it Yourself » · You can declare many variables in one statement. Start the statement with let or constand separate the variables by comma: let person = "John Doe", carName = "Volvo", price = 200; Try it Yourself » ... In JavaScript, the equal sign (=) is an assignment operator, not an equal to ...
Discussions

variable in a string not working in javascript - Stack Overflow
According to these Mozilla docs, you need to enclose your string in backticks, not single quotes. ... @sunday - glad I could help! If this answer solved your issue, you could 'accept' it by clicking the checkmark near the upvote and downvote buttons to let others know this solution works. More on stackoverflow.com
🌐 stackoverflow.com
Beginner question, referencing string variable (van.js)
[tab1]: Normally JS syntax. Declaring object properties from variables needs square brackets. More on reddit.com
🌐 r/learnjavascript
4
0
May 25, 2024
node.js - Javascript: Not able to store the string in variable - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
VS Code not recognizing variable name in string
Interpolated strings use backticks, not single quotes. More on reddit.com
🌐 r/learnjavascript
12
2
May 28, 2024
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Strings
Handling text — strings in JavaScript - Learn web development | MDN
If the browser doesn't recognize the unquoted text, then an error is raised (e.g., "missing; before statement"). If the browser can detect where a string starts but not its end (owing to the missing second quote), it may report an "unterminated string literal" error, or, in the console, jump to a new line and expect you to finish the string there. If your program is raising such errors, then go back and check all your strings to make sure you have no missing quotation marks. The following will work if you previously defined the variable string — try it now: ... In JavaScript, you can choose single quotes ('), double quotes ("), or backticks (`) to wrap your strings in.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript variable in string
How to Insert Variable Into String in JavaScript | Delft Stack
February 2, 2024 - But for the operation variable, the value is assigned to a placeholder that comes next in the sequence. This placeholder is %d. Hence, here a string data type is being converted to a number. The conversion does not happen. Hence the result returned is NaN, which we see as the end word of the output. We can use expressions in the parameters, as we saw in the case of (op1 + op2). The javascript understands and resolves it as a sum accordingly.
🌐
SheCodes
shecodes.io › athena › 8931-creating-a-string-with-variables-in-javascript
[JavaScript] - Creating a String With Variables in | SheCodes
Learn how to add a variable to a string using either string concatenation or string interpolation with template literals for a more modern approach.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-work-with-strings-in-javascript
How To Work with Strings in JavaScript | DigitalOcean
August 24, 2021 - Variables in JavaScript are named containers that store a value, using the keywords var, const or let. We can assign the value of a string to a named variable. const newString = "This is a string assigned to a variable."; Now that the newString variable contains our string, we can reference it and print it to the console. ... This will output the string value. OutputThis is a string assigned to a variable. By using variables to stand in for strings, we do not ...
Find elsewhere
🌐
WebPlatform
webplatform.github.io › docs › concepts › programming › variables_in_javascript
Variables in JavaScript · WebPlatform Docs
For example, if a line terminator ... this leads to an error, because line terminators are not allowed in string literals. You must use \\n for a line feed in a string literal. In JavaScript, the escape sequence works the same way as \\n....
🌐
Reddit
reddit.com › r/learnjavascript › beginner question, referencing string variable (van.js)
r/learnjavascript on Reddit: Beginner question, referencing string variable (van.js)
May 25, 2024 -

I've been experimenting with van.js examples, and overall I really like what I am seeing. Most of my experience in the past has been with modifying simple DOM objects directly, so this framework is promising to me.

The following snippet of code is from the Van UI "Tabs" example, and creates a tab for each entry, revealing the associated text when selected. This is all working fine:

const tab1 = "Test"
console.log(tab1)

  {
    tab1: p(
      "Welcome to ", b("VanJS"), " - the smallest reactive UI framework in the world.",
    ),
    "Getting Started": [
      p("To install the ", b("VanJS"), " NPM package, run the line below:"),
      pre(code("npm install vanjs-core")),
    ],
    About: p(
      "The author of ", b("VanJS"), " is ",
      a({href: "https://github.com/Tao-VanJS"}, " Tao Xin"), "."
    ),
  },

My goal is to use that variable, so that the first tab would display with the name "Test". I have tried many combinations of `{tab1}`, `${tab1}`, etc...but nothing does the trick.

I'm not sure if this is just basic js syntax that I am missing on how to insert the value, or something framework specific. Essentially I have arrays of data that I want to be able to reference in various HTML output.

Ultimately my goal with this example is to get to the point where I can define the title and the contents in an object, and write something that iterates a new tab for each value pair.

🌐
Stack Overflow
stackoverflow.com › questions › 21199355 › javascript-not-able-to-store-the-string-in-variable
node.js - Javascript: Not able to store the string in variable - Stack Overflow
I am trying to a read the string "----\n@-v-\n----\n--^-" in the input variable. console.log("Program Started"); var input = "-->v\n@-^-\n---^"; //not ending with console.log("reading"); var output = getDistance(input); if(output === -999){ console.log("Invalid input string"); } else if(output === -1){ console.log("Infinite loop"); } else{ console.log("Distance covers by beam: "+output+" blocks"); } console.log(output);
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › software development
How to work with strings in JavaScript | Pluralsight
May 19, 2025 - Backticks are especially useful for creating strings with embedded variables or expressions. ... JavaScript offers several properties and methods to manipulate strings effectively. Returns the number of characters in a string. ... Returns the character at the specified index in the string. ... Combines one or more strings and returns a new string.
🌐
Reddit
reddit.com › r/learnjavascript › vs code not recognizing variable name in string
r/learnjavascript on Reddit: VS Code not recognizing variable name in string
May 28, 2024 -

I am working in VS Code learning about the ternary operator and chaining them. I am not sure why VSC isn't recognizing my reference to my soup variable in the string and is returning the entire line as a string. According to MDN I wrote it correctly.

let soup = "Chicken Noodle Soup";
let isCustomerBanned = false;
let soupAccess = isCustomerBanned
    ? "Sorry, no soup for you!"
    : soup
    ? 'Yes, we have ${soup} today.'
    : "Sorry, no soup toady.";

console.log(soupAccess);
🌐
DEV Community
dev.to › sprite421 › adding-variables-into-strings-using-javascript-s-template-literals-16m2
Adding Variables into Strings using JavaScript's Template Literals - DEV Community
December 20, 2020 - Unlike regular strings, we can slot expressions directly into template literals, meaning that we can effortlessly pull values into our string. If we use ${ and } to enclose the name of a variable, and that variable's value will be spliced into our string.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Declare String Variables - JavaScript - The freeCodeCamp Forum
September 11, 2023 - Tell us what’s happening: Describe your issue in detail here. I don’t understand what I am doing wrong here with stringing “myName” with a variable ex. Darko. Please advise. Your code so far var darko = myFirstName ; var rudnik = myLastName ; Your browser information: User Agent is: ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Variables
3 weeks ago - But in the old times, it was technically possible to create a variable by a mere assignment of the value without using let. This still works now if we don’t put use strict in our scripts to maintain compatibility with old scripts. // note: no "use strict" in this example num = 5; // the variable ...
🌐
Codecademy
codecademy.com › forum_questions › 516db5a1ac9fa9882c001583
An efficient way to add a variable $var into a string | Codecademy
The syntax “StringStuff {$var} MoreStringStuff” is an efficient way to add a variable $var into a string. <!DOCTYPE html> <html> <head></head> <body> <?php $verb = "were"; $flipCount = 0; $last = "flips"; echo "<p>There {$verb} {$flipCount} {$last}!</p>"; ?> </body> </html> Why is it efficient? I will prefer to NOT use the curly braces (i.e.
🌐
TutorialsPoint
tutorialspoint.com › how-to-declare-string-variables-in-javascript
How to declare String Variables in JavaScript?
September 14, 2023 - But for declaring a string variable we had to put the string inside double quotes or single quotes. Here's how you can declare strings in JavaScript - ... <html> <head> <title>JavaScript Strings</title> </head> <body> <script> var re = "java"; var str = "Learn java"; if ( str.search(re) == -1 ){ document.write("Not found!"