template_string

Elegant function

traditional

$('#result').append(
  'There are <b>' + basket.count + '</b> ' +
  'items in your basket, ' +
  '<em>' + basket.onSale +
  '</em> are on sale!'
);

template String:

$('#result').append(`
  There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em>
  are on sale!
`);

tag template

let a = 5;
let b = 10;

tag `Hello ${a + b}`;

tag(['Hello', 'World', ''], 15, 20, 25);

function tag(strArr, ...args) {
  let newStr = '';
  strArr.forEach(function(current, index) {
    newStr += current;
    newStr += ' ';
  });
  let num = 0;
  args.forEach(function(current, index) {
    num += parseInt(current);
  });
  console.log(newStr + num);
}

results matching ""

    No results matching ""