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

OUTPUT:

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