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
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);
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.
Learn Programming Books:
Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.
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:
- Learning JavaScript Course on OjamboShop.com
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning JavaScript Paperback on Amazon
- Learning JavaScript Ebook on Amazon
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials