JavaScript Template String

JavaScript Literal String

Create Template String In JavaScript

In JavaScript, the template string (template literal) is a string that allows expressions to be embeded within the string.

Template strings enable more readable and flexible string handling. Regular strings are enclosed in double quotes or single quotes. Template literals use backticks and allow embedded variables.

Template strings can be multi-line without the use of the newline character for double quoted strings.

Common Syntax Of Template String In JavaScript

Glossary:

Unexpanded Literal

Textural representation of a string value using single quotes

Expanded Literal

Textural representation of a string value using single quotes allowing escape sequences

Heredoc

File literal or input stream literal that is treated as a separate file

String Interpolation

Automatic replacing of expressions with real values

Common Template String

Ojamboshop.com Learning JavaScript Template String
Name Description Example
Unexpanded Literal Single quote enclosed string. ‘Hello World’;
Expanded Literal Double quote enclosed string. “Hello World”;
String Literal Backtick enclosed string. `Hello World`;
Name Description Example

JavaScript Template String Snippet

// OjamboShop.com Learning JavaScript Template String Tutorial
let name = "John";
let str = `Hello World`;
console.log(str);
// Quotes Inside String
str = `The man said "Hello World"`;
console.log(str);
// Multiline String And Heredoc
str = `This
is a
Multline string`;
console.log(str);
// Variable Substitution
str = `The man named ${name} said "Hello World"`;
console.log(str);

JavaScript Template String Code
OjamboShop.com Web IDE JavaScript Template String Code

JavaScript Template String Result
OjamboShop.com Web IDE Displaying JavaScript Template String Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the template string.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Get the Learning JavaScript Course for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Learn Programming Books:

Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.

OjamboShop.com Learning JavaScript Book Announcement
OjamboShop.com Learning JavaScript Ebook And Paperback Announcement

Conclusion:

JavaScript makes it easy to use the template string. Use the template string fore more flexible string handling and readable code.

If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books

References:

Leave a Reply

Your email address will not be published. Required fields are marked *