개발공부/Javascript
#기본기6_문자열 내에서 변수 사용하기
킴승
2024. 4. 2. 22:51
템플릿리터럴(template literal)
템플릿리터럴 사용하면 뭐가 좋을까?
- 특정 문자열이 담긴 변수를 동적으로 사용할 수 있게된다.
- str1 + srt2 와 같이 문자열을 연결할 때 보다 가독성이 좋다.
사용방법
문자열 안에 변수를 사용하기 위해서는 백틱(`)으로 문자열을 감싸고 ${} 구문을 사용하여 변수를 삽입한다.
const apple = "Iphone";
const samsung = "Galaxy";
function templateTest (someString) {
const variable = `${someString} is the best!`
console.log(variable)
return variable
}
const result1 = templateTest(apple);
const result2 = templateTest(samsung);