String Interpolation Concept In JavaScript

Ankur TripathiAnkur Tripathi
2 min read

interpolation meaning - The addition of Something in the middle of a text.

interpolation meaning in JavaScript - In JavaScript, interpolation is the process of inserting strings or values into an existing string for various purposes. In JavaScript, the ${} is used to insert a variable into a string.

Interpolation is also called Template Literal, Template String or String Template

example:

Traditional Method (Using Concatenation Method)

const name="Ankur Tripathi"
const company = "LearnCodeOnline"
let salary = 25000
let increment = 5000
let mail = 'webdevankurtripathi@gmail.com'

console.log("Employee name is" +name + "and the company is" +company)
console.log("Salary of "+ name + "after increment "+ increment +" is "+(salary+increment))
console.log("Contact details of "+ name + "is "+mail)

NOTE: plus (+) sign will work as an addition operator only when on its left side and its right side numbers are present in other cases it will work as a concatenator.

Using Interpolation Concept:

const name="Ankur Tripathi"
const company = "LearnCodeOnline"
let salary = 25000
let increment = 5000
let mail = 'webdevankurtripathi@gmail.com'

console.log(`Employee name is ${name} and the company is ${company}
Salary of ${name} after increment is ${increment}:${salary+increment}
Contact details of ${name} is ${mail}`)

Anything written in a backtick will be treated as Interpolation or Template String and it will display the output as it is which means if there is a space in a particular string then in the output window also there will be a space.

2
Subscribe to my newsletter

Read articles from Ankur Tripathi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankur Tripathi
Ankur Tripathi

I am an aspiring full stack web developer @ ineuron