[solved] \n vs \r What's the grammar rule here

[solved] \n vs \r What's the grammar rule here
0

#1

Why would I use a Carriage return vs a next-line escape sequence?
I guess they have their own uses…


#2

I always use \n - you can split strings on \n but apparently not on \r

let input = `this
and
this
and
this`

console.log(input.split('\n'));
console.log(input.split('\r'));
console.log(input.split('\u000A'));
console.log(input.split('\u000D'));

Afaik, these escapes were once actual control codes for physical line-printers and you would need to send both the ‘line feed’ and the ‘carriage return’ to put the print head at the start of the next line. :slight_smile:


#3

Windows uses \r\n, and you don’t generally use \r for much except that, specifically for compatibility with Windows.

In turn, it’s like that for compatibility with older tech. Typewriters do a carriage return to return the head to the start of the line, then move the paper up so the head sits on a new line - \r is carriage return, \n is new line. Older printers were just electronic typewriters, worked in the same way, MS-DOS supported them.

\n is what you’d use. Any text editor should be able to convert automatically, and web browsers are fine with it (Linux rules the web, not Windows). If you’re building Windows software on non-Windows systems the issue comes into play, or if you’re importing text files created by some windows programs onto a different environment (say a Mac), or vice versa.


#4

You guys are talking about Dot Matrix printers?
As man I remember printing reports for school!

I got it. Thanks guys.


#5

Usually, yes. Occasionally, \r\n is still useful. For example, if you’re creating a CSV file with JavaScript, the CSV spec requires it:

Each record is located on a separate line, delimited by a line break (CRLF).

CRLF = carriage return (\r) + line feed (\n).