How to Create JavaScript Multiline String Literals (Template Literals)

That's the hard way to do multi-line strings. Quote to start, quote to end, multiple lines each ending with a backslash will do it.

Even better, use template literals.

Outstanding suggestion! Thanks for sharing.

REF: Template literals - JavaScript | MDN

The linked article did NOT provide a good example, so here is the one I built for myself:

###Using JavaScript Template Literals

var str1 = "This is string 1";
var str2 = "and this is string 2";

var strLit = 
  `some text at the beginning
  and then first string ${str1}
  and then second string ${str2}
  and the end`
  
console.log(strLit)

//### RESULTS ###
/* some text at the beginning
  and then first string This is string 1
  and then second string and this is string 2
  and the end */
  
// NOTE that the TAB character IS included in the resultant string.

Meh. Neither way is hard when you use a macro to generate it, which I do. I honestly don't care either way.

Even better, use template literals

I don't believe template literals are supported on Yosemite.

ES6 only, but certainly clean and pleasing.

(Babel JS will backtranslate, if needed)

Good point. Nor is ES6.
But I think after Sierra has been out for a year or so, I'm going to move up my required macOS to El Capitan+ in my published scripts, although I will still try to avoid using ES6 features unless I really need them.

See my response, well, kind-of response, here:

Hate to quibble (:smirk:) but it‘s also a question of legibility and ease of editing, not just creating. But sounds like a cool macro.

OK, truth time. I don’t remember the exact reason why I use this method, but I remember that I used to use the other method you mentioned (the non-template method), and something about it wasn’t the way I wanted it to be. If I had to guess, my guess would be that Atom auto-formatted it poorly.

But, as to editing, I would almost never edit an action or group’s plist manually (there’s an exception to this, but that’s for another time and place). I always start with the actual action or group, translate that into a plist, then run the plist through my macro to make a string.

So, while I’d certainly change something minor, like a string of some sort, I’d never do any real editing to the string. And that, if you’ve read this far, is the reason I don’t care how which method I use, because I always generate the string.

Glad you asked? :wink:

Fro some idea of what is reasonable to require, rough stats are:

50% using 10.12
30% using 10.11
10% using 10.10

Zipf’s law (rank frequency distribution) usually gives a workable first guess at these things.

If there were four or more successive levels of the operating system active in the wild, you would normally expect the distribution to roughly halve at each level:

1/2 + 1/4 + 1/8 + 1/16 etc etc

So even in two years, it will still be sensible to paste-copy any code to be shared through Babel JS, to bring it down to a more widely shared ES5 base level.

i.e. nothing stops us from writing in ES6 and sharing in ES5 now – there’s no particular value in waiting.

Thanks for that, Peter.

Is that based on KM users, or other data?

That is shocking! Where did you find the stat?

I agree, I was surprised so many folks are on 10.12 already. The data is for Keyboard Maestro users - based on server access. What’s interesting too is that there are still 10% on 10.9 or less, which explains the steady stream of "I purchased Keyboard Maestro 7, but need to use version 6 or 5 or sometimes even 4 or 3.

Macs have a long tail, but still a surprising number on the latest version.

Perhaps a pattern of slightly earlier adoption amongst visitors to the Keyboard Maestro site ?

Another site is getting this distribution: