美文网首页
8. 字符串拼接和模板字符串

8. 字符串拼接和模板字符串

作者: yaoyao妖妖 | 来源:发表于2021-01-12 14:53 被阅读0次

    字符串拼接
    const a = 20
    const b = 10
    const c = 'javascript'
    ES5

    const str = 'my age is' + (a + b) + 'i love' + c
    

    ES6

    const str = `my age is ${a + b} i love ${c}`
    console.log(str)
    

    ES5字符串变量

    const retailPrice = 20
    const wholeSalePrice = 16
    const type = 'retail'
    let showTxt = ''
    if (type === 'retail') {
      showTxt = '您此次的购买单价是:' + retailPrice
    } else {
      showTxt = '您此次的购买的批发价是:' + wholeSalePrice
    }
    console.log(showTxt)
    

    ES6模板字符串

    function Price (strings, type) {
      let s1 = strings[0]
      const retailPrice = 20
      const wholeSalePrice = 16
      let showTxt
      if (type === 'retail') {
        showTxt = '购买单价是:' + retailPrice
      } else {
        showTxt = '购买的批发价是:' + wholeSalePrice
      }
      return `${s1}${showTxt}`
    }
    
    let showTxt = Price`您此次的${'retail'}`
    console.log(showTxt)
    

    ES6换行

    let s1 = `我是第一行
    
    我是第二行`
    
    console.log(s1)
    

    学习视频记录

    相关文章

      网友评论

          本文标题:8. 字符串拼接和模板字符串

          本文链接:https://www.haomeiwen.com/subject/luhqjktx.html