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
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 ...
🌐
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. ... if I have a variable outside a function, do I need to redefine that variable inside the function? similarly, can use the same name for a variable if one is in a function? variable function scope outer variable local variable let const ... How do I make a javascript event where you click on a thumbnail image and it pops up with a full sized image?
People also ask

How to declare a string in JavaScript
Declaring a string in JavaScript means defining a variable that will hold a string value. To declare a string variable in JavaScript, you use one of the following keywords: var, let, or const.
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › software development
How to work with strings in JavaScript | Pluralsight
How to create strings in JavaScript
JavaScript provides multiple ways to create strings, single quotes, double quotes, backticks (template literals).
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › software development
How to work with strings in JavaScript | Pluralsight
What are strings in JavaScript?
In JavaScript, a string is a data type used to represent text. Whether it's a single character or an entire document, strings allow you to work with textual data.
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › software development
How to work with strings in JavaScript | Pluralsight
🌐
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: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15 Challenge: Basic JavaScript - Declare String Variables Link to the challenge:
🌐
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 you have a numeric variable ... Try this: ... These constructs can be really useful in some situations. For example, if a user enters a number into a form's text field, it's a string....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-a-string-with-variables-in-javascript
How to Create a String with Variables in JavaScript? - GeeksforGeeks
November 12, 2024 - To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods.
🌐
TutorialsPoint
tutorialspoint.com › How-to-declare-String-Variables-in-JavaScript
How to declare String Variables in JavaScript?
<html> <head> <title>JavaScript Strings</title> </head> <body> <script> var obj = new String("Welcome to Tutorials Point"); document.write(obj); </script> </body> </html> ... ES6 introduced template literals that allow you to define a string using backtick(`) characters as shown below ? var str = `In elementary school we learn "basic skills" like reading and writing`; In the example below, we define a string variable using the backtick.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › what-is-a-string-in-javascript
What is a String in JS? The JavaScript String Variable Explained
November 7, 2024 - We use strings to represent text-based data and we define them using either single quotes ('), double quotes ("), or backticks (``): let name1 = 'John Doe'; let name2 = "John Doe"; let name3 = `John Doe`; In the above example, you have three ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-work-with-strings-in-javascript
How To Work with Strings in JavaScript | DigitalOcean
August 24, 2021 - In this article, we went over the basics of working with strings in JavaScript, from creating and displaying string literals using single and double quotes, creating template literals, concatenation, escaping, and assigning string values to variables.
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › software development
How to work with strings in JavaScript | Pluralsight
May 19, 2025 - Declaring a string in JavaScript means defining a variable that will hold a string value. To declare a string variable in JavaScript, you use one of the following keywords: var, let, or const. Here's a basic example of declaring a string using let:
🌐
WebPlatform
webplatform.github.io › docs › concepts › programming › variables_in_javascript
Variables in JavaScript · WebPlatform Docs
An alternative method of retrieving a number from a string is with the + operator. "1.1" + "1.1" = "1.11.1" (+"1.1") + (+"1.1") = 2.2 // Note: the parentheses are added for clarity, not required. You use variables as symbolic names for values in your application.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript variable in string
How to Insert Variable Into String in JavaScript | Delft Stack
February 2, 2024 - For instance, for displaying a numeric value, the placeholder in the sentence should be %d for string data type it is %s. let op1 = 5, op2 = 10, operation = 'Sum'; console.log('%s of %d and %d is %d.', operation, op1, op2, (op1 + op2)); ... ...
🌐
W3Schools
w3schools.com › js › js_strings.asp
JavaScript Strings
You can use quotes inside a string, as long as they don't match the quotes surrounding the string: let answer1 = "It's alright"; let answer2 = "He is called 'Johnny'"; let answer3 = 'He is called "Johnny"'; Try it Yourself » · Templates were introduced with ES6 (JavaScript 2016).
🌐
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.

🌐
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 - If you've been browsing this series, ... parts of JavaScript, you've seen how console.log() can be the quickest way to test your code as you work. You may (quite often) need to log the value of a variable and a label that makes the data more readable. The + operator is probably the most obvious way to tack strings together -- ...
🌐
KIRUPA
kirupa.com › javascript › combining_strings_variables.htm
Combining Strings and Variables
Learn how to combine strings made up of static text and variable values using both the concatenation and template literal approaches.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Variables
Storing the information you need — Variables - Learn web development | MDN
JavaScript is a "dynamically typed language", which means that, unlike some other languages, you don't need to specify what data type a variable will contain (numbers, strings, arrays, etc.). For example, if you declare a variable and give it a value enclosed in quotes, the browser treats the variable as a string:
🌐
Freewebdesigntutorials
freewebdesigntutorials.com › javaScriptTutorials › jsStringObject › declaringStrings.php
Declare a String in JavaScript
We can explicitly declare a string ... can also be passed to the constructor. // Declare string variable with string constructor var carter1 = new String("I learn JavaScript"); // Declare an empty string var carter2 = new String(); alert( "|"+ carter2 +"|" );...
🌐
DEV Community
dev.to › lavary › four-ways-to-check-if-a-variable-is-a-string-in-javascript-with-examples-3ilm
Four ways to check if a variable is a string in JavaScript (with examples) - DEV Community
May 10, 2023 - It simply returns a string value representing the data type of its operand. For instance, typeof "hello" returns "string". This is how you can use typeof to test if a variable is a string: let str = 'hello'; if (typeof str === 'string') { ...