Are JavaScript template literals guaranteed to call toString()?

后端 未结 3 1414
慢半拍i
慢半拍i 2020-12-19 22:31
const num = 42
const str = `My number is ${num}`

In this code what guarantee do I have about the conversion of num to a string

3条回答
  •  青春惊慌失措
    2020-12-19 22:40

    Yes. If either value is not a string, it’ll be converted to a string using the usual rules. For example, if action is an object, its .toString() method will be called.

    Read the full article from mozilla https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/

提交回复
热议问题