rafr3 rafr3 - 3 months ago 21
Javascript Question

When to use special sequences \b \f and \r in JavaScript

I know how

works but I have trouble to understand these three special sequences in javascript

- \b (backspace)

- \f (form feed)

- \r (carriage return)

For example for two first sequences I get as output a string with a "strange" char. For
I get
for string
, so it do nothing.

When to use these three escape sequences?


These ASCII control characters have a long historical legacy that is probably a bit off topic for this question, but it's important to understand that they have slightly different meanings or sometimes no meaning in different contexts.

\b - One thing to keep in mind is that characters are used for both input and output. Whereas \b is not really useful in output, if you are reading input, you may find capturing a backspace useful in some scenarios.

\f - Form feed is historically used to advance something like a printer to the next page, so it's not really relevant to most situations. Sometimes some consoles may do something useful when they interpret these, but there's no standard behavior for modern terminal emulators.

\r - The carriage return is still important for some operating systems (especially Windows) because newline (\n) has not always had an implied carriage return associated with it. What this means for modern terminals is that Windows, when getting a \n moves the cursor down a row, but not back to the first column. Thus, end-of-lines have historically been the sequence \r\n so that you both advance to the next line and return to column 1. Unix systems generally use just \n for both making \r obsolete. In Node, you can use os.EOL to get the appropriate string to use for end-of-lines.