Introduction to JavaScript – Variables: String Interpolation

You are here: Home » Blog » Computer Programming Languages » JavaScript » Course 1 - Intro To JavaScript » Introduction to JavaScript – Variables: String Interpolation

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

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

 

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.