|
| 1 | +--- |
| 2 | +Title: 'Template Literals' |
| 3 | +Description: 'Define strings enclosed in backticks that allow embedded expressions using dollar sign and curly braces and support multi-line text in JavaScript.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'ES6' |
| 9 | + - 'JavaScript' |
| 10 | + - 'Strings' |
| 11 | + - 'Syntax' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-javascript' |
| 14 | + - 'paths/full-stack-engineer-career-path' |
| 15 | +--- |
| 16 | + |
| 17 | +**Template literals** are [string](https://www.codecademy.com/resources/docs/python/strings) literals in JavaScript that allow embedded expressions, multi-line strings, and more readable string formatting. Introduced in ES6, they make string manipulation more powerful and expressive than traditional concatenation. |
| 18 | + |
| 19 | +Template literals are enclosed by **backticks** (`` ` ``) instead of single or double quotes. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +Template literals use backticks (`` ` ``) to define the string. To embed expressions, use `${expression}` within the template literal. The syntax is as follows: |
| 24 | + |
| 25 | +```pseudo |
| 26 | +`string text` |
| 27 | +
|
| 28 | +`string text ${expression} string text` |
| 29 | +
|
| 30 | +`string text line 1 |
| 31 | + string text line 2` |
| 32 | +``` |
| 33 | + |
| 34 | +- Backticks (`` ` ``): Define the template literal. |
| 35 | +- `${expression}`: Placeholder for JavaScript expressions, which can include variables, calculations, or function calls. |
| 36 | +- Multi-line support: Line breaks within backticks are preserved, eliminating the need for `\n`. |
| 37 | + |
| 38 | +## Using Template Literals |
| 39 | + |
| 40 | +Template literals simplify string concatenation by allowing variables or expressions to be embedded directly: |
| 41 | + |
| 42 | +```js |
| 43 | +const name = 'Oscar'; |
| 44 | +const greeting = `Hello, ${name}!`; |
| 45 | +console.log(greeting); |
| 46 | +``` |
| 47 | + |
| 48 | +The output of this code is: |
| 49 | + |
| 50 | +```shell |
| 51 | +Hello, Oscar! |
| 52 | +``` |
| 53 | + |
| 54 | +In this example, the variable `name` is embedded using `${}`. This is more readable than traditional concatenation (`"Hello, " + name + "!"`). |
| 55 | + |
| 56 | +## Multi-line Strings |
| 57 | + |
| 58 | +Template literals make multi-line strings straightforward without needing explicit newline characters: |
| 59 | + |
| 60 | +```js |
| 61 | +const poem = ` |
| 62 | + Roses are red, |
| 63 | + Violets are blue, |
| 64 | + JavaScript is fun, |
| 65 | + And so are you! |
| 66 | +`; |
| 67 | +console.log(poem); |
| 68 | +``` |
| 69 | + |
| 70 | +The output for this is: |
| 71 | + |
| 72 | +```shell |
| 73 | + |
| 74 | + Roses are red, |
| 75 | + Violets are blue, |
| 76 | + JavaScript is fun, |
| 77 | + And so are you! |
| 78 | +``` |
| 79 | + |
| 80 | +The output preserves the line breaks as written, making it ideal for formatting text like HTML or poetry. |
| 81 | + |
| 82 | +## Expression Interpolation |
| 83 | + |
| 84 | +Template literals can include any valid JavaScript expression inside `${}`: |
| 85 | + |
| 86 | +```js |
| 87 | +const a = 6; |
| 88 | +const b = 12; |
| 89 | +const result = `Sum = ${a + b}`; |
| 90 | +console.log(result); |
| 91 | +``` |
| 92 | + |
| 93 | +The output here is: |
| 94 | + |
| 95 | +```shell |
| 96 | +Sum = 18 |
| 97 | +``` |
| 98 | + |
| 99 | +Expressions can also include function calls or complex calculations: |
| 100 | + |
| 101 | +```js |
| 102 | +const getName = () => 'Oscar'; |
| 103 | +const message = `Hi, ${getName()}! Your score is ${Math.random() * 100}.`; |
| 104 | +console.log(message); |
| 105 | +``` |
| 106 | + |
| 107 | +The output of this code is: |
| 108 | + |
| 109 | +```shell |
| 110 | +Hi, Oscar! Your score is 87.62799470776743. |
| 111 | +``` |
| 112 | + |
| 113 | +## Tagged Template Literals |
| 114 | + |
| 115 | +Template literals can be combined with a tag function to customize how the strings and expressions are processed: |
| 116 | + |
| 117 | +```js |
| 118 | +function myTag(strings, ...values) { |
| 119 | + return strings[0] + values[0].toUpperCase() + strings[1]; |
| 120 | +} |
| 121 | +const name = 'Oscar'; |
| 122 | +const tagged = myTag`Hello, ${name}!`; |
| 123 | +console.log(tagged); |
| 124 | +``` |
| 125 | + |
| 126 | +The output of this code is: |
| 127 | + |
| 128 | +```shell |
| 129 | +Hello, OSCAR! |
| 130 | +``` |
| 131 | + |
| 132 | +Here, `myTag` is a custom function that manipulates the template literal’s parts. The `strings` parameter holds the static parts, and `values` contains the evaluated expressions. |
| 133 | + |
| 134 | +## Codebyte Example |
| 135 | + |
| 136 | +In this example, template literals are used to embed variables directly into a string. The placeholders `${user}` and `${age}` are replaced with their values when the string is evaluated, producing a readable, formatted output without using concatenation: |
| 137 | + |
| 138 | +```codebyte/javascript |
| 139 | +const user = "Oscar"; |
| 140 | +const age = 25; |
| 141 | +const info = `User: ${user}, Age: ${age}`; |
| 142 | +console.log(info); |
| 143 | +``` |
0 commit comments