首页 > 解决方案 > Template literals (Template strings) didn't give the exact result in arrow function

问题描述

I am following a medium article about javascript es 6 template literals. But it didn't give me the result, but I type the exact code that showed in the article. I think maybe something silly that I had mistaken, but didn't able to find out.

const myFunctn=(name,age)=>{
return 'Hi ${name} , you are ${age} years old';
}
console.log(myFunctn('Said',22));

ok i think the output should have

Hi Said, you are 22 years old

But my output in the chrome browser console is below

Hi ${name} , you are ${age} years old

I had checked that ECMAScript is enabled in my browser, so that is not the issue. Maybe I had make some stupid mistakes.

标签: javascriptecmascript-6

解决方案


您需要反引号作为模板文字的分隔符。

const myFunctn = (name, age) => {
    return `Hi ${name} , you are ${age} years old`;
}

console.log(myFunctn('Said', 22));


推荐阅读