Question about \n and \r - Escape Sequences in Strings

Tell us what’s happening:
This exercise wants you to make a string that looks this:

FirstLine
\SecondLine
ThirdLine

What I don’t understand from the answer given is that, why after “FirstLine” you use a “\n” but after “\SecondLine” you use a “\r”? How can you tell the difference whether the ending of a line is “\n” or “\r”? And how do you decide when to use which?

Your code so far

var myStr="FirstLine\n\\SecondLine\\\rThirdLine"; // Change this line


Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/escape-sequences-in-strings

/n and /r works similarily, I read it can be used based by your OS (Windows, Macs, Unix).

\n is used as an end-of-line terminator in Unix text files
\r is used as an end-of-line terminator in Mac text files
\r\n (ie both) are used to terminate lines in Windows and DOS text files.

You can read more about it here:

1 Like

If you are asking how do you know which one to use to pass the challenge, the answer is the instructions spells it out for you with the following.

image