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:

But the instructions given are:

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?

Difference between newline and carriage return?

Hi. Perhaps this will answer your question.


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


From this link on stackoverflow

  • in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), \r was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, \r\n, in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows), \r\n is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like “terminals”, \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) – that’s the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-)
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)

In practice, in the modern context of writing to a text file, you should always use \n (the underlying runtime will translate that if you’re on a weird OS, e.g., Windows;-). The only reason to use \r is if you’re writing to a character terminal (or more likely a “console window” emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy “ascii animation” effects of e.g. progress bars) – this is getting pretty obsolete in a world of GUIs, though;-).

That’s the “difference” and historically why it made sense to do it that way. Common practice now is just use \n - UNLESS you specifically need (in a program that runs from the command line in a terminal) to continually rewrite over one line, instead of skipping down to the next. If you do a bunch of installing stuff on linux, you’ll see this a lot, where there’s one line with a progress bar or byte count or percentage complete getting updated continually while a process is running.

Why did you have to use a carriage return for this specific exercise? The only reason is because it was a contrived example meant to show you that both newline and carriage return escape sequences exist, so they’ll be “on your radar” in case you see them in code somewhere.

Javascript quests: Escape Sequence in Strings

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


@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.”



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:


Clipboard paste result:



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





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”.