문자열

자바스크립트에서 문자열을

var word1 = 'apple'
var word2 = "banana"

로 표현했다.

하지만 ` ` 을 통해서도 표현이 가능하다.

이 방법을 이용하면 1. 엔터키를 사용할 수 있다.

var sentence = `hello, my
name is jinho`;

또 중간에 ${ }를 이용하여 변수를 넣기 편리하다.

var name = 'jinho';
var sentence = `hello my name is ${name}.`;

함수와 변수를 같이 사용할 수 도 있다.

Tagged literal

var name = 'jinho';
var sentence = `hello my name is ${name}.`;

function myfunction(words, variables){
  console.log(words);  //1
  console.log(variables);  //2
}

myfunction`hello my name is ${name}.`

1번으로 인해 [“hello”, “my”, “name”, “is”, “.”] 가 출력되고, 2번으로 인해 jinho 가 출력된다.

파라미터의 첫번째인 word는 문자들을 Array화 해주고 두번째인 variables 는 ${변수} 를 뜻한다.

연습문제

var pants = 20;
var socks = 100;

`바지${pants} 양말${socks}`
// 바지 20 양발 100  이 출력. 여기서 20과 100을 함수를 사용해 바꿔보자.

function myfunction(word, var1, var2){
  console.log(word[0] + var2 + word[1] + var2)
}

myfunction`바지${pants} 양말${socks}`