Introduction to JavaScript – Variables: String Interpolation

Introduction to JavaScript – Variables: String Interpolation

In previous exercises, we assigned strings to variables. Here, you will learn how to insert the content saved to a variable into a string.

The JavaScript term for inserting the data saved to a variable into a string is string interpolation.

The + operator, known until now as the addition operator, is used to interpolate (insert) a string variable into a string, as follows:

let myPet = 'armadillo';
console.log('I own a pet ' + myPet + '.');
// Output: 'I own a pet armadillo.'

In the example above, we saved the value ‘armadillo‘ to the myPet variable. On the second line, the + operator is used to combine three strings: I own a pet, the value saved to myPet, and . (period). We log the result of this interpolation to the console as:

Introduction to JavaScript – Variables: String Interpolation


I own a pet armadillo.

Related Posts:

Introduction to JavaScript – Control Flow

Introduction to JavaScript – Control Flow: if/else Statements

Introduction to JavaScript – Built-in Methods

Introduction to JavaScript – Libraries

Introduction to JavaScript – Variables: Undefined

Introduction to JavaScript – Data Types

Introduction to JavaScript – Review Types and Operators

How to make a Go-Back Input button with inline JavaScript

Using the Current Year in Your Posts – WordPress

CODING WITH CSS: The style attribute

The color CSS property

Introduction to JavaScript – CONSOLE

Tips and Tricks for WRITING JAVASCRIPT code

Learn more about JavaScript Variables

Introduction to JavaScript – Variables: Review

Learn about JavaScript ELSE STATEMENTS

Learn about JavaScript IF STATEMENTS

Introduction to JavaScript – Control Flow: True and False values


Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.