Template Literals

์ •์Šน์›ยท2023๋…„ 3์›” 31์ผ
0
post-thumbnail
post-custom-banner

๐Ÿ“’ ๋ชฉ์ฐจ

  • ๐Ÿ“Œ Template Literals์ด๋ž€?
  • ๐Ÿ“Œ Syntax
  • ๐Ÿ“Œ ์ฐธ์กฐ

๐Ÿ“Œ Template Literals์ด๋ž€?

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ํ‘œํ˜„์‹/๋ฌธ์ž์—ด ์‚ฝ์ž…, ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด, ๋ฌธ์ž์—ด ํ˜•์‹ํ™”, ๋ฌธ์ž์—ด ํƒœ๊น…๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

์ด์ „ ES2015์—์„œ๋Š” template strings๋ผ๊ณ  ๋ถˆ๋ €๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ์ผ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌ ๋ฐ ๋ณ€ํ™˜์ด ๋œ๋‹ค.

ํ”„๋ก ํŠธ์—”๋“œ์—์„œ๋Š” HTML์„ ๋ฐ์ดํ„ฐ์™€ ๊ฒฐํ•ฉํ•ด์„œ DOM์„ ๋‹ค์‹œ ๊ทธ๋ ค์•ผ ํ•˜๋Š” ์ผ์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•œ๋‹ค.

์ด๋•Œ, ํ…œํ”Œ๋ฆฟ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ํŽธ์ง‘ํ•˜๊ณ  ์ž‘์„ฑํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๊ธฐ์— ์ด์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด ์ƒ๊ธฐ๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ(") ๋Œ€์‹  ๋ฐฑํ‹ฑ(`)(grave accent)๋กœ ๊ฐ์‹ธ์ค€๋‹ค.

๋˜ํ•œ, ๋ฌธ์ž์—ด๊ณผ ๋ณ€์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ์ฝ”๋“œ์˜ ๊ฐ€๋„์„ฑ์ด ๋†’์œผ๋ฉฐ, ์ง๊ด€์ ์œผ๋กœ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿ“Œ Syntax

    `string text`

    `string text line 1
     string text line 2`

    `string text ${expression} string text`

    tag `string text ${expression} string text`
const nation = "Korea";

const literal1 = `Hello World`;

const literal2 = `Hello ${nation} World`;

๐Ÿ“Œ ์ฐธ์กฐ

https://eblee-repo.tistory.com/38

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Template_literals

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€