Difference between newline(\n) and carriage-return(\r) escape sequences?

This is about the ‘Escape Sequences in Strings’ challenge inside ‘Basic Javascript

I have to assign the following value to a variable myStr:
FirstLine
\SecondLine
ThirdLine

But the instructions given are:
“FirstLine-newline-backslash-SecondLine-backslash-carriage-return-ThirdLine”

Why can’t I just use a newline escape sequence instead of carriage-return? Since all I have to do is just go to the next line below.
Why is carriage-return used and not newline?

6 Likes

Hi. Perhaps this will answer your question.

5 Likes

So, according to that, whatever you use it is right but \n is just like two moves. Am I getting it right?

Thank you very much! That was a great answer :smiley: I get that

2 Likes

@P1xt Thanks for the detailed explanation! It cleared up all the confusion.

Here is how I’m writing the code example:

var myStr = “Firstline\n\Secondline\\rThirdline”;

In the lesson, the check requires there to be only one \n. The \r doesn’t seem to function properly. I have tried different work arounds and searching hasn’t provided any direction. I have tried using ‘console’ in the developer window of the browser and it appears \n is working but the \r is not.
According to the rules of the instruction,

var myStr = “Fristline\n\Secondline\\r\nThirdline”;

is not valid by the lesson standards.

Is the carriage-return not working?
Am I misunderstanding the lesson?

I think this lesson has a bug.

@BBLThumper You need three \'s after Secondline, you have two so you are escaping the backslash and then printing ‘r’ as a regular character.

When I copy and pasted all the required backslashes were present, the editor removed the others.

According to the book ‘Javascript Bible’ by Danny Goodman, chapter 15 ‘The String Objects’, section ‘Special Inline Characters’ states:

“In most cases, browsers ignore carriage returns or render them like spaces.”

https://books.google.com/books?id=WMr2NEG88icC&pg=PT262&lpg=PT262&dq=javascript+carriage+return&source=bl&ots=dWFLqN0rlO&sig=eZGo_74AHZq_4e2pxmtiqXz6l9s&hl=en&sa=X&ved=0ahUKEwivlMaEqu_QAhWBOSYKHSviBM44HhDoAQgjMAI#v=onepage&q=javascript%20carriage%20return&f=false

To highlight this issue I put similar isolating code as is in the lesson inside console.log();
I received some interesting results.

This is what occurs in the browser as displayed in console:

escape characters use a
backslash. But a carrige-return does this!

This is the same code copied to clipboard from the browsers console display and pasted into an editor:

escape characters use a
backslash. But a carrige-return
does this!

Browser display:

Firstline
\Secondline\Thirdline

Clipboard paste result:

Firstline
\Secondline
Thirdline

I was wondering if anyone figured out @BBLThumper’s issue? On my chrome console I get the same results using /r:

Browser display:

var myStr = "Firstline\n\\Secondline\\\rThirdline"; console.log(myStr);

output:

Firstline
\Secondline\Thirdline

Hi,

this was confusing me too and that really helped, thanks :slight_smile:

I think it is because you use small ‘L’ in the “Firstline, Secondline and the Thirdline”, it should be like this “FirstLine, SecondLine, and ThirdLine”.

Thanks.