Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript multiline strings compile into multiline strings

Tags:

coffeescript

How come that this string

"answer   to life   the universe   and everything  is  #{40+2} " 

compiles into

"  answer   to life   the universe   and everything  is  " + (40 + 2) + ""; 

how can I force coffescript to keep it multiline (keeping string interpolation intact):

 "answer \   to life \  the universe \  and everything \  is \  "+(40+2) 
like image 970
iLemming Avatar asked Oct 15 '13 19:10

iLemming


People also ask

How do I create a multiline string in JavaScript?

There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.

How do you do multiline strings?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.


1 Answers

Try using the heredoc syntax:

myString = """ answer to life the universe and everything is #{40+2} """ 

This converts to this javascript:

var myString;  myString = "answer\nto life\nthe universe\nand everything\nis\n" + (40 + 2); 

There's not really any point to make it actually be on newlines in the compiled javascript visually, is there?

like image 135
nzifnab Avatar answered Oct 01 '22 20:10

nzifnab