How to Do String Interpolation in JavaScript

In this tutorial, we will delve into the concept of interpolation and illustrate its application in enhancing the strength of strings. You will also learn how to utilize string interpolation to enhance the quality of your programming code. Furthermore, we will provide various code snippets showcasing the utilization of JavaScript strings for interpolation to further solidify your understanding.

Introduction of JavaScript Interpolation

One significant benefit of programming languages is the capability of string interpolation, which permits the insertion of variables, function invocations, and arithmetic operations directly within a string. Before ES6, JavaScript lacked string interpolation. ES6 introduced a new functionality for string interpolation, enabling the creation of multi-line strings without the need for an escape character. It simplifies the usage of apostrophes and quotation marks, contributing to improved readability of our strings and code. There are several reasons supporting the preference for string interpolation over string concatenation.

String Interpolation in JavaScript

In JavaScript, template literals enable the concatenation of values into a string. To achieve this, you use a dollar sign followed by a pair of curly brackets. The expression you want to insert the value of within the string needs to be enclosed in curly braces.

Values can be inserted into a string by utilizing template literals as opposed to concatenation. Instead of enclosing your string with quotation marks, employ backticks (``) to define a template literal.

Syntax

The string that contains interpolation enclosed within backticks is referred to as expression.

Placeholders in JavaScript enable the flexible inclusion of inputs within string sections using string interpolation, which differs from the traditional approach of concatenating variables with strings. Take a look at the syntax provided:

Example

'It is a string to add the 5+6: ${5 + 6}.'

The following expression is defined as a template literal.

Example

${5+ 6}

The calculation "5 + 6" is computed as the outcome of this mathematical expression. This outcome is due to the expression being entered prior to the termination point.

Examples

Below are some illustrations demonstrating the utilization of string interpolation in JavaScript.

Example 1:

Illustrated below is a simple demonstration of JavaScript string interpolation. This technique involves substituting expressions within the interpolation. It is worth noting that string interpolation supports various operations including addition and mathematical calculations.

Example

<!DOCTYPE html>
<html>
<head>
<title> How to do string interpolation in JavaScript? </title>
</head>
<body style = "background-color:beige;">
<h2> How to do string interpolation in JavaScript? </h2>
<h4> Basic javascript string interpolation with the expression substitution </h4>
<b id = "data_view"> </b>
<br>
<b id = "data_view1"> </b>
<script>
let basic_var = `Addition Value: ${9+19}`;
document.getElementById("data_view").innerHTML = basic_var;
let price_var = 20;
let VAT_var = 0.25;
let total_var = `Total Value: ${(price_var * (1 + VAT_var)).toFixed(2)}`;
document.getElementById("data_view1").innerHTML = total_var;
</script>
</body>
</html>

Output

The output shows the javascript interpolation.

Example 2:

In the provided instance, we illustrate the fundamental concept of string interpolation in JavaScript, where we employ string substitution within the interpolation process. The interpolation function in JavaScript allows for the utilization of both string and arithmetic operators.

Example

<!DOCTYPE html>
<html>
<head>
<title> How to do string interpolation in JavaScript? </title>
</head>
<body style = "background-color:beige;">
<h2> How to do string interpolation in JavaScript? </h2>
<h4> Basic javascript string interpolation </h4>
<b id = "data_view1"> </b>
<script>
const name = "Ram Verma";
const company  = "Example";
let salary = 8000;
let increment = 2100;
let mail = 'abdul@gmail.com';
let total_var = `Employee name is ${name} and the company is ${company}
Salary of ${name} after increment is ${increment}:${salary+increment}
Contact details of ${name} is ${mail}`;
document.getElementById("data_view1").innerHTML = total_var;
</script>
</body>
</html>

Output

The output shows the javascript interpolation.

Example 3:

Demonstrated below is a fundamental illustration of string interpolation in JavaScript. By creating an array, we can utilize interpolation within the string to exhibit a value.

Example

<!DOCTYPE html>
<html>
<head>
<title> How to do string interpolation in JavaScript? </title>
</head>
<body style = "background-color:beige;">
<h2> How to do string interpolation in JavaScript? </h2>
<h4> Basic javascript string interpolation </h4>
<b id = "data_view1"> </b>
<script>
let header_var = "JavaScript Strings Interpolation";
let tags_var = ["HTML", "javascript", "es6", "Jquery"];
let html_var = `<h2>${header_var}</h2><ul>`;
for (const x of tags_var) {
html_var += `<li>${x}</li>`;
}
html_var += `</ul>`;
document.getElementById("data_view1").innerHTML = html_var;
</script>
</body>
</html>

Output

The output shows the javascript interpolation.

Example 4:

An illustration below demonstrates the fundamental concept of string interpolation in JavaScript. By crafting a JavaScript function, we can leverage string interpolation to execute operations effectively.

Example

<!DOCTYPE html>
<html>
<head>
<title> How to do string interpolation in JavaScript? </title>
</head>
<body style = "background-color:beige;">
<h2> How to do string interpolation in JavaScript? </h2>
<h4> Basic javascript string interpolation with the functions </h4>
<b id = "data_view"> </b>
<br>
<b id = "data_view1"> </b>
<br>
<b id = "data_view2"> </b>
<script>
// javascript String Interpolation
function myData(first_name, last_name, country_name) {
return `My name is ${first_name} ${last_name}. ${country_name} is my favorite country`;
}
document.getElementById("data_view").innerHTML = myData("Ram", "doe");
document.getElementById("data_view1").innerHTML = myData("Shyam", "Sharma", "India");
//create a basic function and use string interpolation
function greet() {
return "Hello Developers!";
}
document.getElementById("data_view2").innerHTML = `${greet()} I am a computer science student.`;
</script>
</body>
</html>

Output

The output shows the javascript interpolation.

Example 5:

Below is an illustration demonstrating fundamental string interpolation in JavaScript. When a condition is incorporated into a function along with logical and mathematical operations, the resulting string is displayed as output.

Example

<!DOCTYPE html>
<html>
<head>
<title> How to do string interpolation in JavaScript? </title>
</head>
<body style = "background-color:beige;">
<h2> How to do string interpolation in JavaScript? </h2>
<h4> Basic javascript string interpolation with the functional condition </h4>
<b id = "data_view"> </b>
<br>
<b id = "data_view1"> </b>
<script>
// javascript String Interpolation
function isOddEven(x) {
document.getElementById("data_view").innerHTML = `x is ${x%2 === 0 ? 'even' : 'odd'}`;
document.getElementById("data_view1").innerHTML = `x is ${x > 3 ? 'Greater than 3': 'Less than 3'}`;
}
isOddEven(5); // x is odd
</script>
</body>
</html>

Output

The output shows the javascript interpolation.

Conclusion

String interpolation facilitates the direct operation of functionality and operators, allowing for the simultaneous handling of data, output, and operational tasks. This feature greatly accelerates and simplifies the development of applications.

Input Required

This code uses input(). Please provide values below: