0

I have got a string from an ajax call. That variable directly comes from a text field from the MySQL database. But after getting that value I'm unable to console log it. By Console debug it is saying multiline error.

I'm giving the variable which I need to console.log.

var str = "Hello Sir/Madam
            Please find password sent on your email.<br><br>\n545abea<br><br>




            Warm Regards,";

how to console.log what comes from AJAX?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • You can use template strings, though you may need to transpile your code depending on where it will be used. – Fissure King Aug 15 '18 at 16:12
  • 1
    If he receives this via Ajax, where does he put those? – Daniel W. Aug 15 '18 at 16:17
  • @DanFromGermany, good point and I think that bears further explanation. OP, please update your question with exactly how that variable is set, your specific `console.log` invocation, and the actual error it throws. – Fissure King Aug 15 '18 at 16:23

4 Answers4

5

You can use template literals for Strings that span multiple lines. Template literals are enclosed with backticks (`).

var message = `Hello Sir/Madam
                Please find password sent on your email.<br><br>\n545abea<br><br>
    
    
    
    
                Warm Regards,`;
console.log(message);

For better browser support, you could just concatenate the String and use "\n" for new lines.

var message = "Hello Sir/Madam\n"+
"                Please find password sent on your email.<br><br>\n545abea<br><br>"
+"\n\n\n\n"+       
"                    Warm Regards,";
console.log(message);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
4

As far as I know you can use \

"First Line \
 Second Line \
 Third Line"
1
var str = `Hello Sir/Madam
            Please find password sent on your email.<br><br>\n545abea<br><br>




            Warm Regards,`;

https://codepen.io/pixel-lab/pen/wxbxKO

pixellab
  • 588
  • 3
  • 12
  • 3
    While this might answer the OP's question, please do not post code-only answers; include an explanation of your code and/or solution. – Daniel W. Aug 15 '18 at 16:15
1

You need to use backticks (`) to allow for a multiline string.

var str = `Hello Sir/Madam
            Please find password sent on your email.<br><br>\n545abea<br><br>




            Warm Regards,`;

console.log(str);

https://jsfiddle.net/y5hs9r0b/

The following is invalid syntax and js will be unable to parse it.

var str = "Hello Sir/Madam
            Please find password sent on your email.<br><br>\n545abea<br><br>




            Warm Regards,";

If however you are getting the data from a database, it should already be in a variable in which case you can just console.log the existing var.

Jim Wright
  • 5,905
  • 1
  • 15
  • 34