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

字符串拼接
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. 字符串拼接和模板字符串)