This Gist was automatically created by Carbide, a free online programming environment.
Last active
February 5, 2022 16:38
-
-
Save jerbear2008/26537b1da80cb0486aa39128aab75318 to your computer and use it in GitHub Desktop.
Tagged Template Literals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(a=>{console.log(a[0])}) | |
`test` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((strings) => { // tag for template literal | |
console.log(strings[0]) // log first string | |
})`test` // immediatley use as a tagged template literal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tag; | |
`text` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = 1 | |
let b = '1' | |
console.log(`the value of a is ${a} and the value of b === a is ${b === a}.`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tagOne(strings, ...expressions) { | |
strings = Array.from(strings) | |
let output = strings[0] | |
for (let i in expressions) { | |
i = Number(i) | |
let expression = expressions[i] | |
output += `${typeof expression} ${expression}` | |
output += strings[i + 1] | |
} | |
return output | |
} | |
console.log(tagOne`string example: ${'test' + 'ing'}, number example: ${1 + 2}, the end.`) | |
// string example: string testing, number example: number 3, the end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tag(strings, ...expressions) { | |
// strings is an array-like object with all the string fragments | |
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates:~:text=Tags%20allow,expressions. | |
// expressions is an array of all the expressions within ${} | |
} | |
tag`string1${'expression1'}string2${'expression2'}string3` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tag `text` | |
tag | |
`text` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment