Template Literals

keep in mind...

  1. the $ is NOT jQuery!
  2. uses the weird backtick ` character
  3. reference your variables with ${myCoolVar}
syntaxvariables

Writing out strings with variables inside used to be pretty unwieldy in Javascript, especially with multi-line strings. ES6 added major changes to strings, although you can still use the older method of var + 'myString' as well. I still use that in places where it's shorter and cleaner.

syntax

back-tick: ` Template literals use a funky character that you may be familiar with if you've written code blocks in markdown: the back-tick. While it looks a bit foreign at first, they make strings easy to spot.

edit and re-run!
const name = 'Spot';

 console.log(`my dog is named ${ name }`);    
my dog is named Spot

variables

${ variable } While at first this seems a little complicated for a simple string, at least the variables/expressions are clearly delineated.

edit and re-run!
const firstName = 'Sancho';
const lastName = 'Panza';

console.log(`name is ${ firstName } ${ lastName }`);
name is Sancho Panza

javascript inside You can also so more when them - you can use a ternary expression or even a function inside of your placeholder.

edit and re-run!
const firstName = '';

console.log(`name is ${ firstName ? firstName : 'Bob' }`);        
name is Bob

Research More...

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
  2. https://wesbos.com/javascript-template-strings
  3. https://closebrace.com/tutorials/2018-03-07/js-quick-hits-7-template-literals