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?

3
  • You can use template strings, though you may need to transpile your code depending on where it will be used. Commented Aug 15, 2018 at 16:12
  • 1
    If he receives this via Ajax, where does he put those? Commented Aug 15, 2018 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. Commented Aug 15, 2018 at 16:23

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

4

As far as I know you can use \

"First Line \
 Second Line \
 Third Line"

Comments

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

1 Comment

While this might answer the OP's question, please do not post code-only answers; include an explanation of your code and/or solution.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.