ES6
6. Template Literals
으누아빠
2020. 7. 8. 11:56
반응형
Template Literals
- 기존 문자열 표기법 "문자열", '문자열'
- ES6 에서는 template Literals 문자열 표기법 도입
- ``(백틱)을 이용하는 표기법
- `${변수명}` 형태로 표기 가능
- `${함수명}` 형태로도 표기 가능
ES5
- 문자 + 문자 형태로 연결
console.log('Hello \n' + 'world');
//결과값
//Hello
//world
let str1 = 'hi!'
let str2 = 'kang'
console.log(str1 + ' this is template literal test. ' + str2); // 결과값 hi! this is template literal test. kang
ES6
- 백틱형태로 연결이 가능함
console.log(`Hello
world`);
console.log(`${str1} this is template literal test. ${str2}`); // 결과값 hi! this is template literal test. kang
//결과값
//Hello
//world
//함수도 이용가능
let name ='Jone';
function makeUppercase(word) {
return word.toUpperCase();
}
let template = `${makeUppercase('Hello')}, ${name}`;
console.log(template); //결과값 HELLO, Jone