1

Apologies if this question has already been asked and answered, but I've been looking for an hour or so and haven't find a solution that helped.

Can anyone tell me how to break a long string into two lines in this code within my js file:

    console.log(percentage);
    var message;
    if (percentage === 1) {
      message = 'This is the FIRST line. Then there needs to be a SECOND line BENEATH the first line.'
    } else if (percentage >= .5) {
      message = 'This is the FIRST line. Then there needs to be a SECOND line BENEATH the first line.'
    } 
    } else {
      message = 'This is the FIRST line. Then there needs to be a SECOND line BENEATH the first line.'
    }


  • 1
    Have you had a look at [this](https://stackoverflow.com/questions/508269/how-do-i-break-a-string-across-more-than-one-line-of-code-in-javascript) post? – user8408080 May 18 '20 at 16:23
  • 1
    How hasn't \n worked? I just tried it, and it's working for me in node (i.e. console.log shows the newline) – Holden Rohrer May 18 '20 at 16:27
  • I don't think, that OP wants a linebreak in the output, but rather break the long string in the editor, so it's more readable in the code – user8408080 May 18 '20 at 16:29
  • Thanks for the post, @user8408080. Inserted "\n" , "\", "\r\n" and "\u000A" (one by one) at the end of my first line of text, but a new line still didn't appear when I viewed the output in my browser. Also tried single back quotes (`) around the text - no joy. It's a linebreak in the output that I'm after. – JesusGirl2000 May 18 '20 at 20:12
  • Can you please show your exact code and your exact output then? `\n` should really give you a linebreak – user8408080 May 18 '20 at 20:28
  • Hello @user8408080. Just tried to paste my exact code in for you but StackOverflow says it's too long. Is there another way I can get it to you? – JesusGirl2000 May 19 '20 at 11:07
  • The best way would be to write a minimal example, which reproduces your problem – user8408080 May 19 '20 at 13:01
  • Thanks, @user8408080. All code's working perfectly, just wanted to know what to add (and where) to make 2 lines out of 1 in the code that I originally pasted in. I'd hoped that adding "\n" or "\r\n" or something similar directly into that part of the code would achieve that, but it didn't. – JesusGirl2000 May 19 '20 at 13:34
  • Well, if I just put `console.log("abc\ndef")` in the console, I get a linebreak between `abc` and `def`. So something has to be going wrong on your side or I didn't quite understand, what you are doing – user8408080 May 19 '20 at 13:38
  • Thanks @user8408080. If I can get that linebreak to appear in my browser, it'll be job done! That's what I'm trying to achieve - the linebreak appearing in the output in the browser. – JesusGirl2000 May 19 '20 at 14:42
  • So what gets printed in your browser console if you type `console.log("abc\ndef")`? – user8408080 May 19 '20 at 15:11
  • You are right, @user8408080. That does produce a second line in my browser's console log. Sorry, I should have been clearer. The output I'm trying to get the 2 lines with is the output of the "message" variable on the webpage itself, not in the console log. – JesusGirl2000 May 19 '20 at 16:30
  • I think you should edit the question and state your exact question with example code, that reproduces the problem – user8408080 May 19 '20 at 20:03

0 Answers0