Template Literals

By  on  

Seemingly every language has a template string enhancement, and within JavaScript we've written our own to allow more seamless development without the need to concatenate everything.  Some of these template helpers even allow for looping, iteration, and conditional support.  Native implementations always start small but I'm excited about template literals in JavaScript!

The JavaScript

The template format is very simple:  backticks(`) instead of single or double quotes, and a $ for interpolation wrapping:

// Basic interpolation
var name = 'David';
console.log(`Hi, my name is ${name}`); // Hi, my name is David

// Math :)
var one = 1;
var two = 2;
console.log(`Your total is: ${one+two}`); // Your total is: 3

// More math
console.log(`Another total is: ${one + two * 2}`); // Another total is: 5

// Object properties
var obj = { x: 1, y: 2 };
console.log(`Your total is: ${obj.x + obj.y}`); // Your total is: 3

You can also use template strings for basic new line acceptance:

var myString = `Hello

I'm a new line`; // No error!

The JavaScript template string feature is a nice add, and will first become available in Firefox.  This template string feature isn't groundbreaking but it's a nice enhancement and something long overdue, if only for multi-line strings.

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    9 More Mind-Blowing WebGL Demos

    With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities.  I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...

Incredible Demos

  • By
    Making the Firefox Logo from HTML

    When each new t-shirt means staving off laundry for yet another day, swag quickly becomes the most coveted perk at any tech company. Mozilla WebDev had pretty much everything going for it: brilliant people, interesting problems, awesome office. Everything except a t-shirt. That had to change. The basic...

  • By
    MooTools 1.3 Browser Object

    MooTools 1.3 was just released and one of the big additions is the Browser object.  The Browser object is very helpful in that not only do you get information about browser type and browser versions, you can gain information about the user's OS, browser plugins, and...

Discussion

  1. Isuru
    var name = 'David'; 
    var me = 'Isuru';
    console.log(Thank you Mr. ${name}! Great to know this. :) - ${me});
    
    • Ha! Great one Isuru!

      Thanks David! :D

  2. Steve

    2 questions:

    1.) What happens for undefined/null variables? Are they swapped for blanks or …?

    var foo = "Cars";
    //var bar = 37;
    var baz = null;
    
    alert(`You have ${bar} ${foo} left in your ${baz}!`);
    >>> a.) "You have ${bar} Cars left in your ${baz}!"
    >>> b.) "You have undefined Cars left in your null!"
    

    2.) Just to clarify… are the templates “evaluated” when the string variable is defined? or at runtime when the line is evaluated?

    var count = 0;
    
    function doIt(){
      alert(`You have ${count} new messages`);
    }
    
    count = getMessageCount();//returns 53
    
    doIt();
    
    >>> "you have 0 new messages"
    >>> "you have 53 new messages"
    
    • > What happens for undefined/null variables?

      The same as what happens when they are concatenated normally.

      > are the templates “evaluated” when the string variable is defined? or at runtime when the line is evaluated?

      The line is evaluated at runtime, when the string variable is defined. Remember JS doesn’t do a compile step. In your example, every time the doIt function is called, it creates a new template string. When the template string is created, the template string references will be evaluated and interpolated.

    • Gaurav Khurana

      they are evaluated as string null or undefined ;
      var test = null, greet = ‘hello world’, obj=undefined;
      ${test} ${greet} ${obj} // “null hello world undefined”
      you may use this to ignore them
      ${test||''} ${greet} ${obj||''} ; //” hello world “

  3. Multi-line strings is definitely something to look forward to – I’ve been using them in CoffeeScript for years.

    I have to say using ` and ${} is a bit weird, where other languages simply use " and #{}.

    • Can’t use those because of backwards compatibility.

  4. The backticks are pretty horrible, especially since there doesn’t seem to be a reason to not just keep both ‘ and ” for templating. Both are active strings already, accepting \t and \n and the like as macros (the most minimal form of a template macro). Given the proliferation of mustache, I’d have expected something like “string with {{varContent}}”. No reason to invent a new quoting symbol (and ` is an diacritic, not a quote mark of any sort. TeX made that mistake in the 80s, but has since recovered), and arguably, no reason to invent a new syntax for the templating macros when the world’s already using one that’s highly popular =(

    • > No reason to invent a new quoting symbol

      Why do you think they picked the new syntax? I’m sure that the first thing they thought of was reusing existing syntax, but not breaking existing websites is a pretty good reason to invent a new quoting symbol. A standard set of substitutions like \t and \n is not at all comparable to accepting arbitrary variables and expressions.

      What you are suggesting would break and potentially expose cross-site scripting vulnerabilities in websites expecting quoted strings to be inert. Future changes to JS need to be backwards compatible with older versions, at least those still in service – this even means some affordances for avoiding conflicts with popular libraries e.g. MooTools vs Array.prototype.contains: https://esdiscuss.org/topic/having-a-non-enumerable-array-prototype-contains-may-not-be-web-compatible

      There’s little point raging about this, it’s done and happening. If you can’t or won’t change something there’s little point complaining about it. And at the end of the day what does it matter which quoting symbol is used? It doesn’t matter at all.

  5. Emil

    Question: Is there any other use for this than getting a multiline string?

    
    Is this just the easiest way
    to get a multiline string into js?
    
    

    Also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings#Tagged_template_strings

  6. Emil

    So the code above should be wrapped in a <script type="text/template"> tag.

    Tagged templates could actually help with exactly this, just safeHtml ${userinput} :)

  7. In case your wondering about performance. :)
    http://jsperf.com/template-strings-perf/3

  8. balaganesh

    Hey David,

    call function using template literals

    //from https://michelenasti.com/2018/09/19/Javascript-chiamare-funzioni-senza-usare-parentesi-(what!).html
    function hello(name) {
    	console.log(How are you ${name});
    }
    
    // The convention is to write the string right 
    // after the function name...
    helloMichele 
    //-> How are you Michele 
    
    //...but you can put a space too 
    hello Michele 
    //-> How are you Michele

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!