When to use the {js| |js} quoted string?


#1

It appears that usage of {js| |js} and {j| |j} are similar, but with the latter supporting variable interpolation.

Could we always just use {j| |j} or are there some cases when it would be better to use {js| |js}?

Thanks


#2

The only obvious case I can think of is when you don’t want to escape the dollar signs, plus it shows right away there’s no string interpolation in this string, which can improve code readability.

But maybe there are other reasons too.


#3

I could imagine js is faster to parse and should be preferred when you have really big strings where you need unicode support, but that can possibly only be answered by @bobzhang or any other person with deeper insights into the compiler.


#4

I thought of that too, but it seems it’s actually parsed at build time, so there’s probably no difference at runtime.

let foo = {js|foo|js};
let bar = {j|bar|j};
let baz = {j|Hello $foo|j};

outputs

var foo = "foo";
var baz = "Hello " + (String(foo) + "");
var bar = "bar";

#5

Yeah, sorry. I was referring to build time. So it’s probably negligible for most people.


#6

@tsnobip @fham thanks :pray:


#7

Use j at most time, only use js when your quoted string has {j| as well which would cause a lexing issue but it’s rare in practice