Does JavaScript have literal strings?

25,400

Solution 1

Short answer: No

Long answer: Noooooooooooooooooooooooooo

Solution 2

I don't know what you're getting at, but one way to get around the problem of escaping (etc) is use a trick that John Resig seems to like a lot. You include <script> blocks in a page, but give them a "type" like "text/plain" to make sure that the browser doesn't hand them over to Javascript. Then use the text of the script block for whatever you like.

<script id='a_string' type='text/plain'>
  Here is some stuff.
  There might be some \escape sequences in it.
</script>

Then you can grab that with $('#a_string').text() (or with getElementById if you're not using jQuery or something like that).

edit: Here's John Resig's explanation about why dropping stuff into script blocks like that is a good idea:

Quick tip: Embedding scripts in your page that have a unknown content-type (such is the case here - the browser doesn't know how to execute a text/html script) are simply ignored by the browser - and by search engines and screenreaders. It's a perfect cloaking device for sneaking templates into your page. I like to use this technique for quick-and-dirty cases where I just need a little template or two on the page and want something light and fast.

Taken from this page: http://ejohn.org/blog/javascript-micro-templating/

Solution 3

Just escape the escapes

var myCrazyString = "\\yes\\we\\have\\no\\bananas"
Share:
25,400
DevelopingChris
Author by

DevelopingChris

I'm a software developer mainly doing web based applications.

Updated on August 23, 2021

Comments

  • DevelopingChris
    DevelopingChris almost 3 years

    In C#, Ruby, and many other languages you can denote a string as to not need escaping. In C# it’s like this

    string s = @"\whatever\this\is";
    

    The results are when printed:

    \whatever\this\is
    

    Is this supported in any form in JavaScript?

  • Syntactic
    Syntactic about 14 years
    What is the advantage of this over using some other random DOM element hidden from view with CSS? It seems like it breaks the semantics of the script element.
  • Pointy
    Pointy about 14 years
    I don't know, frankly; I've been trying to figure that out. I first came across it in examples for the new jQuery "template" stuff. I agree that it seems squirrely, and especially so when you consider what jQuery does with script blocks when you dynamically insert content via "html()". Still, it's a thing that people do; I'm not sure what the OP is trying to discover or achieve.
  • Pointy
    Pointy about 14 years
    @Syntactic I've updated the answer with a snippet from Resig's blog
  • qbolec
    qbolec over 10 years
    I think that this is better than using, say, hidden <div> to accomplish the task, in that you are allowed to use "<" and ">" more carelessly. If the template is just a valid HTML then sure you can use <div> to enclose it. If however the template is written in some templating language (handlebars, underscore, etc) which supports conditional expressions (if, try, catch) and loops (for, for in, foreach, map, ...) you can quickly end up with a string blob which does not have balanced opening and closing tags, violates nesting rules, has unfinished attributes etc.
  • Pointy
    Pointy over 10 years
    @qbolec agreed. When I wrote that comment on my answer, I didn't know as much as I do now :) Also, jQuery doesn't strip script blocks that have an explicit type attribute that's non-JavaScript (like "text/html").
  • MandM
    MandM almost 9 years
    The question specifically requested "is this supported in any form in javascript" and was referring to "denot[ing] a string as to not need escaping". Adding more escapes is kind of the opposite as not needing escapes...
  • sidgeon smythe
    sidgeon smythe over 6 years
    I have reverted the recent edit that completely changed this answer, to claim that template literals can be used as literal strings. For a bunch of reasons: (1) To radically change an answer in this way is misleading, (2) there is already another answer (by Iain Reid below) proposing template literals, (3) as commented on the other answer, template literals are not literal strings; for instance you cannot put a string containing \useless or ${what is this} inside one of them.
  • Mogsdad
    Mogsdad over 6 years
    There's absolutely no reason to modify this answer - "it shows up in search engines" isn't justification.
  • Peter Mortensen
    Peter Mortensen almost 3 years
    Perhaps elaborate in your answer why template literal strings can't be used for this.