Can you create a JavaScript string without using ' or " quotes?

前端 未结 5 1639
生来不讨喜
生来不讨喜 2020-12-15 12:51

I have a JS file with some XML in it, where the XML is supposed to get converted to a word by the server.

E.g.

var ip = \"

        
5条回答
  •  被撕碎了的回忆
    2020-12-15 13:14

    In JavaScript, you can escape either type of quote with a \.

    For example:

    var str = "This is a string with \"embedded\" quotes.";
    var str2 = 'This is a string with \'embedded\' quotes.';
    

    In particular, your block of JavaScript code should be converted to:

    var ip = "$(VAR{'ip_addr'})";
    

    In general, I always prefer to escape the quotes instead of having to constantly switch quote types, depending upon what type of quotes may be used within.

提交回复
热议问题